socket connection a string ASCii 0

Discussion in 'Mac Programming' started by ctalmeida, May 17, 2011.

  1. ctalmeida, May 17, 2011
    Last edited by a moderator: Jun 4, 2011

    macrumors newbie

    May 17, 2011
    Sorry the ignorance, complete new guy on cocoa, coming from a complete different programming background.

    I need to send in a socket connection a string ASCii 0 (asc zero not the number zero) plus a letter, the letter will be any letter.
    All the socket programming I have done but I just cannot find how to create this 2 byte string to send it.

    Thanks for any help.
  2. Moderator emeritus


    Jul 24, 2002
    Don't use Cocoa strings. If you want to use raw ASCII characters it's probably more sensible just to use raw C strings.
  3. macrumors 68000


    Aug 17, 2009
    But ASCII zero is the termination character, it cannot appear in C-type strings. Perhaps your best bet would be to create a NSMutableData object and put the bytes you want to send into it. If you try to tell C to send a string that starts with a zero, AFAIK, it will just say, oh, nothing to send.
  4. Moderator emeritus


    Jul 24, 2002
    A C string is just an array of characters. There is nothing preventing you creating an array of chars that starts with \0 followed by a single additional character. You would then be able to send that.
  5. gnasher729, May 17, 2011
    Last edited: May 17, 2011

    macrumors G5


    Nov 25, 2005
    A C string is not just an array of characters. It is a zero-terminated array of characters. Anything following the \0 will be ignored.

    ASCII zero followed by any letter? You mean a letter a, or ä, or ⷣ, or �� ? I suspect what you actually want to do is send two bytes. You can use Posix sockets in Objective C. Or you use NSOutputStream, which has the write:maxLength: method.

    NSOutputStream* myStream = ...;
    char data [2] = { 0, 'X' };
    [myStream write:(void *) data, 2];
  6. SidBala, May 17, 2011
    Last edited: May 17, 2011

    macrumors 6502a

    Jun 27, 2010
    ASCII zero, '0', is decimal 48. That is not the termination character.

    The null string termination character, '\0', is decimal 0.

    Since the OP is asking for '0'. I don't think there should be any problem.
  7. macrumors 65816


    Dec 16, 2010
    Quoting the OP (emphasis added):
    I think the OP does want to send ASCII NULL '\0' not ASCII zero '0'. So a C string will indeed be problematic.

    I agree that an NSMutableData would be the best way to go. Especially because it's toll-free bridges with CFData, which makes it directly usable within CFNetwork, particularly CFSocketSendData.

    Or just use an array of chars. But treaded as that, an array of chars, not as a C string.
  8. thread starter macrumors newbie

    May 17, 2011
    socket connection a string ASCii 0

    Thank you very much for all the help.

    Actually I found the solution and is very simple. Is just a question to know all the ways in a language, what takes a long time.

    NSString *string=@"\x00\x4C";

    Thanks again to all.

  9. macrumors 68030


    Dec 8, 2003
    I believe the "C String can't have null byte in it" is only for using functions that rely on the null byte for their own purposes, like telling the length, or printing to a file etc. Namely the functions in <string.h>
  10. macrumors 604

    Aug 9, 2009
    As it turns out, the OP seems to have wanted an Objective-C NSString, not a C string.

Share This Page