Re: NSString and control-@ character
Re: NSString and control-@ character
- Subject: Re: NSString and control-@ character
- From: Alastair Houghton <email@hidden>
- Date: Tue, 4 Dec 2007 10:46:37 +0000
On 3 Dec 2007, at 23:47, Ricky Sharp wrote:
On Dec 3, 2007, at 5:23 PM, C Sandeep wrote:
Im a newbie to Objective-C and Cocoa. I need to send control-@
character
over a socket, for a wire protocol that Im working on.Im using
NSString to
encode the control-@ character as @"\0", but it didn't work. I have
google'd
and searched this mailing list to no avail. Any help regarding this
very
much appreciated. Thanks.
Don't use strings as control codes are either not allowed, or not
possible to store depending upon the encoding. Use raw bytes of
data instead. Depending upon the API being used, you can use NSData
to wrap your raw bytes.
That's not quite right. Control codes (below ASCII 32) are valid in
almost every character encoding (aside from EBCDIC) because everyone
rather sensibly based the area below 128 on 7-bit ASCII. If this
weren't the case, you wouldn't be able to use strings with characters
like '\r', '\n' or '\t' in them, which would be a disappointment to
most people I should think :-)
The problem is most probably that the NUL character is being
interpreted as a string terminator by something. (But NS and
CFStrings can contain NUL characters, because most of their
representations are stored with an explicit length... after glancing
at the CFString code, I think there's a problem with NS/CFString
constants, but there actually shouldn't be with Cocoa/Core Foundation
strings generally.)
Anyway, you're right that using NSData is more appropriate here.
Doubly so because the wire protocol will specify which character
encoding you are to use, so any actual string data (as opposed to NUL
bytes) will need converting to that encoding.
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