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.
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 to
memcpy`.
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.