0
votes

How do i convert integer to bytes string (NSString of bytes) ,

I used the below code char barr[4];
barr[0] = (unsigned int) (num & 0xff);
barr[1] = (unsigned int) ((num >>8) & 0xff);
barr[2] = (unsigned int) ((num>>16) & 0xff);
barr[3] = (unsigned int) (num >>24);

NSString *str = [NSString stringwithformat:@"%x",barr];

but i am getting "BFFFF648" but not in binary data which i want such that NSlog will print unreadable binary data?

1
it's not clear to me what you want to do. can you provide an example? - Chris Cleeland

1 Answers

2
votes

If you actually want a byte string: NSString is not the right data structure to represent bytes. It's meant to represent text, and is subject to conversion between encodings and various other transformations. To represent a string of bytes, use NSData. (For instance, something like [NSData dataWithBytes:&myInteger length:sizeof(myInteger)].)