Re: string encoding in NSMutableData
Re: string encoding in NSMutableData
- Subject: Re: string encoding in NSMutableData
- From: "Paolo Manna" <email@hidden>
- Date: Wed, 8 Aug 2007 17:34:31 +0100
> can't figure out how to successfully encode a Unicode string as hex into an NSMutableData.
> The data is meant to contain "a9415254" for the string "@ART" (QuickTime Metadata programming)
Then it's probably better to avoid Unicode: what you probably want is
to get the 4 bytes that represent as closely as possible the ASCII
string...
> output = [NSMutableData dataWithBytes:[key cStringUsingEncoding: NSUnicodeStringEncoding]
> length:4];
> }
> Instead of what I expect, the result is...
> a9006100
> Which appears to be the string, but with lots of padding :)
Yes, Unicode in this context is synonym for UTF-16, i.e. 16 bits per
char: you can use UTF-8 (NSUTF8StringEncoding), that uses 8 bits
(actually 7) for ASCII chars and a combination of bytes with the
high-bit set for non-ASCII, but this will NOT guarantee the length of
the bytes buffer will always be 4 bytes, so you'd lose some
information and, most important, end up with something that makes no
sense!
Not knowing what you're using your data for, if you want to get 1 byte
for each character you input, you'd better use something like
NSASCIIStringEncoding: it's lossy anyway, but in a predictable way.
However, as the matter has multiple solutions, I'd strongly suggest a look at:
http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/index.html
Paolo
_______________________________________________
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