if i have a NSString as @"aef1230fe" how can I map it to NSData exacaly so that when doing this:
NSLog("%@",mydata);
gives me the output: <aef1230fe>
if i have a NSString as @"aef1230fe" how can I map it to NSData exacaly so that when doing this:
NSLog("%@",mydata);
gives me the output: <aef1230fe>
Unshifting and shifting.
Step one: Get the byte string from your NSString, this is achieved using -UTF8String
.
Step two: Read each character in the string, mapping them to their corresponding quartet:
for (char *iterator = myByteString; *iterator != '\0'; iterator++) {
switch *iterator {
case 0:
quartet = 0x00;
break;
case 1:
quartet = 0x01;
break;
/// .... Optimizations exists
}
}
Step three: Shift your quartet up every second time, and or it with the other one:
octet = quartet1 << 4 | quartet2;
Step four: Stick your new octet in a buffer to hold the data (this buffer should be malloced to [dataString length]
/ 2).
Step five: Turn the buffer into an NSData object.
As mentioned in the comment above, optimizations exist for the switch
; namely the use of quartet = *iterator - 'a'
(or '0'
, or 'A'
, depending on what segment of the switch you are in).
Of course, you might be able to finagle something using the property list serialization API as well (or rather, deserialization).