1

I am trying to convert the byteArray to a Hex NSString.

Here is the solution that I referred to convert it into hex NSString. But, I discovered It add's ffffffffffffff. How can I get correct hex NSString?

Best way to serialize an NSData into a hexadeximal string

const char myByteArray[] = {
        0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,
        0x12,0x23,0x34,0x45,
        0x56,0x67,0x78,0x89 };

    NSData *myByteData=[NSData dataWithBytes:myByteArray length:sizeof(myByteArray)];
    NSMutableString *myHexString= [NSMutableString stringWithCapacity:myByteData.length*2];
    for(int i=0;i<myByteData.length;i++){
        ;
        NSString *resultString =[NSString stringWithFormat:@"%02lx",(unsigned long)myByteArray[i]];
        [myHexString appendString:resultString];
    }

The output String

12233445566778ffffffffffffff8912233445566778ffffffffffffff89
Community
  • 1
  • 1
NNikN
  • 3,720
  • 6
  • 44
  • 86
  • What is the NSData for? You never use it so why did you create it? – matt Jan 30 '16 at 04:40
  • In the link that I had mentioned NSData was used to get length. So, I had added, but actually there is no need. – NNikN Jan 30 '16 at 04:50

2 Answers2

1

Don't use unsigned long for each of your bytes. And what's the point of myByteData if you don't use it?

And since you are not really using char, use uint8_t.

Try this:

const uint8_t myByteArray[] = {
    0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,
    0x12,0x23,0x34,0x45,
    0x56,0x67,0x78,0x89 };

size_t len = sizeof(myByteArray) / sizeof(uint8_t);
NSMutableString *myHexString = [NSMutableString stringWithCapacity:len * 2];
for (size_t i = 0; i < len; i++) {
    [myHexString appendFormat:@"%02x", (int)myByteArray[i]];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Can the length be obtained from size_t len = sizeof(myByteArray)? – NNikN Jan 30 '16 at 04:56
  • 1
    `sizeof(myByteArray)` is the number of bytes. Dividing by the size of each element of the array gives the length. In this case it's the same since each element is one byte. – rmaddy Jan 30 '16 at 04:58
  • yes uint8_t is unsigned char. That would be shorthand notation. – NNikN Jan 30 '16 at 05:18
  • It's more than shorthand. It's more clear. You have a set of unsigned 8-bit integer values. `uint8_t` better expresses that use than does `unsigned char`. – rmaddy Jan 30 '16 at 05:20
0

Your initial byte data is char rather than unsigned char. This means that any values >127 (0x7f) will be seen as a twos-complement negative number, giving ffffffffffffff89.

If you change your data to be unsigned char you will get the desired result.

const unsigned char myByteArray[] = {
        0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,
        0x12,0x23,0x34,0x45,
        0x56,0x67,0x78,0x89 };

NSData *myByteData=[NSData dataWithBytes:myByteArray length:sizeof(myByteArray)];
NSMutableString *myHexString= [NSMutableString stringWithCapacity:myByteData.length*2];
for(int i=0;i<myByteData.length;i++){

    NSString *resultString =[NSString stringWithFormat:@"%02lx",(unsigned long)myByteArray[i]];
    [myHexString appendString:resultString];

}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • If i use const unsigned char, then the for loop condition should be [myHexString appendFormat:@"%02x",myByteArray[i]], this will avoid conversion to unsigned long. – NNikN Jan 30 '16 at 04:54
  • Yes, I don't know why you were converting to a long. – Paulw11 Jan 30 '16 at 04:55