PDA

View Full Version : socket connection a string ASCii 0




ctalmeida
May 17, 2011, 02:21 PM
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.



robbieduncan
May 17, 2011, 02:54 PM
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.

Don't use Cocoa strings. If you want to use raw ASCII characters it's probably more sensible just to use raw C strings.

Sydde
May 17, 2011, 03:26 PM
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.

robbieduncan
May 17, 2011, 03:53 PM
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.

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.

gnasher729
May 17, 2011, 05:44 PM
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.

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.


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.

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];

SidBala
May 17, 2011, 09:08 PM
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.

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.

jiminaus
May 18, 2011, 06:13 AM
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.

Quoting the OP (emphasis added):
string ASCii 0 (asc zero not the number zero) plus a letter

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.

ctalmeida
May 18, 2011, 08:18 AM
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.


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.

jared_kipe
May 18, 2011, 11:17 PM
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>

chown33
May 19, 2011, 12:01 AM
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>
As it turns out, the OP seems to have wanted an Objective-C NSString, not a C string.
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";