Two Questions on NSStrings <-> bytes
Two Questions on NSStrings <-> bytes
- Subject: Two Questions on NSStrings <-> bytes
- From: Jaime Magiera <email@hidden>
- Date: Mon, 22 Oct 2007 12:10:21 -0400
Hello,
I'm still learning on byte writing stuff. Not sure why it's been so
difficult for me. Though I can code web apps, VR tools and scripts
galore, simple byte-level writing is a mental roadblock. :) Here are
two questions that have stumped me...
1. One of the specs I'm coding for requires a Big Endian 8-bit hex
value -- meant to be the length of a C-String. So, I tried the
following...
// value is 20
int8_t chapterURLSizeValue = [chapURLLengthNumber intValue];
NSString *test = [NSString stringWithFormat:@"%
x",chapterURLSizeValue];
NSLog(@"test string: %@", test);
CFIndex lhsLen = [test length];
UniChar *lhsBuf = malloc(lhsLen * sizeof(UniChar));
[test getCharacters:lhsBuf];
data3 = [NSMutableData dataWithCapacity:1];
[data3 appendBytes:&lhsBuf length:1];
free(lhsBuf);
NSLog(@"Data 3: %@", data3);
Result...
test string: 14
Data 3: <e0>
In other words, I'm getting the byte value of the string. How do I
get that hex NSString to a literal byte array? Would there be a
better way to get a 8-bit hex value from an NSNumber?
2. The C-string measured above is meant to come from a url stored in
an NSString in a NSDictionary. To get the byte array I tried the
following...
// string is http://www.apple.com
NSString *chapterURL = [resourceDict objectForKey:@"url"];
const char *cChapterURL = [chapterURL cStringUsingEncoding:
[NSString defaultCStringEncoding]];
[data3 appendBytes:&cChapterURL length:strlen(cChapterURL)];
Result...
50628816 00000005 00000000 a0110090 01000000
Very much incorrect. What would the proper way to get a c-string from
a URL in an NSString?
Overall, does anyone know of a tome that would be useful is learning
byte-level writing and data conversion at that level?
thanks for any direction,
Jaime Magiera
Sensory Research
http://www.sensoryresearch.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden