0

I've been trying to convert a user defaults NSString (tried NSData also), into a hex value I can use with char array. the problem is that every time I convert it from the NSString or NSData it takes the hex values and turns them into ASCII values, which isn't what I want at all. how to I tell it to literally take the values that are there and keep them hex? I'm struggling to find a solution.

inside my .plist

<key>shit</key>
<string>5448495344414e475448494e474e45454453544f53544159484558</string>

my code to convert the NSString to NSData

  NSString *defaults = [[NSUserDefaults standardUserDefaults] objectForKey:@"shit"];
    NSData *theData = [defaults dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [theData length];
    Byte *byteData = (Byte*)malloc(dataLength);
    memcpy(byteData, [theData bytes], len);

NSLog(@"THIS : %@",[NSData dataWithBytes:byteData length:len]);

output:

THIS : <35343438 34393533 34343431 34653437 35343438 34393465 34373465 34353435 34343533 35343466 35333534 34>len

why in the world is it doing this? I want the original hex values, not interpretation of my hex values.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
ctfd
  • 338
  • 3
  • 14
  • @JeremyP, when i try the other solution you pointed out i get the correct value, although if i memcpy(new, old, len), i get a totally different result. any idea why? – ctfd Jan 31 '14 at 14:57
  • Because the string doesn't contain the actual binary vales but the UTF-16 representation of them. – JeremyP Jan 31 '14 at 15:15
  • yes, i noticed it's type wasn't char, but NSMutableData. how can i get it into char []? that would help so much. actually, it would probably solve the issue and you earn a gold star. – ctfd Jan 31 '14 at 15:17
  • @JeremyP UTF16? That is a complication that is not applicable here and perhaps an implementation detail of `NSString`. – zaph Jan 31 '14 at 15:24

2 Answers2

1

A byte is neither hex nor ascii nor octal nor int nor float, etc, they are just different ways to display and/or think about a byte(s). The way NSLog displays objects is dictated by the way the objects's description method is written. Thus NSLog of NSData and NSString display the same bytes differently.

If you want access to the bytes in an NSData just access them directly: theData.bytes, no need tomemcpy`.

Lastly and most importantly: What are you trying to accomplish?

New developers are well served by getting a good book on the "C" language and studying it--that is what I did.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • I'm trying to populate a char array with the hex values such as `char hex[] = {0x54, 0x84, etc.}` i can't seem to get the correct hex values into the array after reading it back from the defaults .plist. putting the value in is no problem, it's getting it out and into my array that is the issue. – ctfd Jan 31 '14 at 14:22
  • if i can get it to be `<54484953 44414e47 5448494e 474e4545 4453544f 53544159 484558>` that would really be so great. – ctfd Jan 31 '14 at 14:25
  • I'm not sure what `aq` books are though, but maybe if you have a copy of the book available that I can maybe borrow? – ctfd Jan 31 '14 at 15:29
  • Oops, type! "aq" should be "a"--fixed. These days we rarely look at the bytes in a computer program, code or data--perhaps it is a lost art. :-) But your confusion (not uncommon) does point out that an underlying understanding can be helpful. There is always the question of how deep to dive. ASCII, UTF encoding and their implementation, floating point and it's bit implementation, even deeper, it's a long way to the bottom. You need to decide your level of depth vs breadth. – zaph Jan 31 '14 at 15:41
0

This is the method I use to get Hex data from string:

-(NSData *) HexByteDataFromString:(NSString *) str
{
    NSMutableData *data= [[NSMutableData alloc]init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    for (int i = 0; i < ([str length] / 2); i++)
    {
        byte_chars[0] = [str characterAtIndex:i*2];
        byte_chars[1] = [str characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [data appendBytes:&whole_byte length:1];
    }
    return data;
}

Now to print the data in NSData, just use this

NSLog(@"%@",[myData description]);

Here, description will give exactly the hex values as you wanted. :)

Update:

Use Byte *dataBytes = (Byte *)[myData bytes]; This will give you the same thing you need. You can refer to particular byte just like you refer them from byte array.

like this:

char x=  dataBytes[2];

Let me know if this is still not solving your problem.. :)

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41