NSString and control-@ character

  • Hi all,

    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.

    - Sandeep
  • 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.

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • 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
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today