Re: Two Questions on NSStrings <-> bytes
Re: Two Questions on NSStrings <-> bytes
- Subject: Re: Two Questions on NSStrings <-> bytes
- From: Alastair Houghton <email@hidden>
- Date: Mon, 22 Oct 2007 17:54:56 +0100
On 22 Oct 2007, at 17:10, Jaime Magiera wrote:
I'm still learning on byte writing stuff. Not sure why it's been so
difficult for me.
You're probably over-thinking it. That's the usual reason for
problems like this.
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...
It doesn't make sense for an eight-bit value to be Big Endian (or
Little Endian). Unless you're talking wire protocols (and only then
if you actually get involved with the hardware itself), a byte is a
byte is a byte. It's only when you get into multi-byte units that
endianness generally becomes a concern.
Anyway, I'm unclear what you need here (and I think the reason is
that *you're* unclear what it is that you need). Do you want a
binary value? Or an ASCII string consisting of two hexadecimal
digits? If you need binary, e.g. in an NSData, all you need do is:
#import <stdint.h>
uint8_t value = 20;
NSData *myData = [NSData dataWithBytes:&value length:1];
(I prefer using uint8_t rather than "unsigned char", because it makes
it clear that we're talking 8-bit bytes, not characters. Hence the
#import, to get that type definition.)
If you want an NSString with the hexadecimal digits, you can do
NSString *myString = [NSString stringWithFormat:@"x", value];
If you need that to be ASCII instead, the simplest way is to use the
C APIs instead, e.g.
char buffer[3];
snprintf (buffer, sizeof(buffer), "x", value);
NSData *asAnNSData = [NSData dataWithBytes:buffer length:2]; //
Two ASCII bytes, not NUL terminated
NSData *withNUL = [NSData dataWithBytes:buffer length:3]; //
Three bytes, last is a NUL
though you can also use NSString, e.g.
NSData *asciiData = [myString
dataUsingEncoding:NSASCIIStringEncoding];
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?
What do you want here? Do you want the number of code points?
(URLs, at least in future, will be able to contain arbitrary Unicode;
I don't know how much support there is currently in Cocoa for that
because I haven't checked, but there seems no reason to assume that
there will be any unnecessary restrictions.)
Or do you want the length assuming some particular string encoding
(e.g. UTF-8)?
If it's just the number of UTF-16 code units that you want, you can
use NSString's -length method. If you need something else, you might
consider -lengthOfBytesUsingEncoding:, with an appropriate argument,
though if you need the data as well then you could just use -
dataUsingEncoding: and then the length of the NSData is the length
you are after.
Overall, does anyone know of a tome that would be useful is
learning byte-level writing and data conversion at that level?
Well, it depends exactly what it is that you want to learn. A good
place to start, for lower-level things like bytes, would be learning
plain, vanilla C (not C++, Cocoa, C#, Java or anything like that, as
tutorials in those languages tend either to assume that you know C,
or try to skirt around the issue entirely). It sounds like you might
also want to take a good look at the whole area of character
encoding, so that you understand why the results you show above are
as they are... you might start by taking a look at ASCII: <http://
en.wikipedia.org/wiki/ASCII>
Finally, much of your confusion here seems to derive from not really
understanding what you're trying to produce. So perhaps you should
ask whoever wrote the spec. you're trying to adhere to to explain
what they meant?
Kind regards,
Alastair.
--
http://alastairs-place.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