Insert NULL in midle of string

How can I insert \x00 or NULL into a string?



If I use:
char sends[9999];

strcpy(sends,"\xFF\xFF\xFF\xFF\x6D");
strcat(sends,ip);
strcat(sends,":");
strcat(sends,port);
strcat(sends,"\x00");
strcat(sends,data;
strcat(sends,"\x00");
sendto(sockfd, sends, sizeof(sends) , 0,(struct sockaddr *)&their_addr, sizeof their_addr))

Doesn't work.

Help!!!
Last edited on
Doesn't work
is not a very informative statement.

You fill sends and then you send ss.
How can I put NULL in midle of string and send or cout or puts()?
If i use:
char ss[]="mircea\0is";
cout<<ss;

Result: mircea
I wanna be the result: mircea NULLcharacter is
closed account (3TXyhbRD)
What is sockfd?
Aren't you just trying to get a substring?
It's a socket.
I wanna send to a server name00data00ip00port something like this
I can't separate name, data, port with \x00
closed account (3TXyhbRD)
Oh you want "mircea\0is" exactly as it is. For that use double black slash like this: "mircea\\0is".
Instead of "\\0", if you display the string, it will be like this: "mircea\0is".
Last edited on
I have the answer to this question and would like to share it.

But since you're likely to just delete your question after you get an answer, I'd rather not waste my time.

Obligatory link:
http://cplusplus.com/forum/general/41290/#msg222972 (last post on page)
Last edited on
Srry for that, this is for a project, and my teacher said that I musn't use the network to search for help.

Now i didn't delete this topic.
Indeed?

In that case we have even less of a reason to help you, if what you're doing now is cheating. O_o

Sorry. :(

-Albatross
Last edited on
i'm not cheating, but the teacher doesn't teach me about udp protocol. I must learn alone.
So the teacher expects you to research this on your own, but you can't use the internet?

That doesn't make any sense.
Last edited on
Silly restriction, imo.
And no one keeps you from using the internet to look something up when working on "real" projects.

Anyway, you might want to use a string or a stringstream. C strings cannot contain \0 since it's used to mark their end, strings however can (use operator += to append your \0s, don't include them in string literals).
Last edited on
@Athar it's work fine but I can't send to server with sendto:

...main.cpp|239|error: cannot convert 'std::string' to 'const char*' for argument '2' to 'int sendto(SOCKET, const char*, int, int, const sockaddr*, int)'
closed account (3hM2Nwbp)
convert 'std::string' to 'const char*'


http://cplusplus.com/reference/string/string/
Yea, but if I convert the char doesn't read/send \x00.
closed account (3hM2Nwbp)
I see...looks like you have a catch-22 going on. The C-String won't send completely if a null character is reached, but you can't send anything except a C-String. Are there any overloads of the sendto(...) method?
I don't know.
I didn't saw any method.

I must send something like this:
data NULL data NULL data NULL
in hex:
 
FF FF FF FF dd d4 d2 1c 00 b4 5c 1c 00 2b 3a 00


00 work like a separator!
my string:

1
2
3
4
5
6
7
string s;
s="\xFF\xFF\xFF\xFF\xDD\xD4\x1C";
s+="\0";
s+="B4\x5C\x1C";
s+="\0";
s+="\x2B\x3A";
s+="\x1C";


1
2
3
4
5
6
7
char data[9999];
strcpy( reinterpret_cast <char*> (data), s.c_str());



        if ((numbytes = sendto(sockfd, data, sizeof(data) , 0,(struct sockaddr *)&their_addr, sizeof their_addr)) == -1)
            {perror("talker: sendto"); cout<<"\nnu s-a trimis!"; return 0; }




I use a sniffer to see what program send out:
.mDataDataData
and hex:
 
FF 6D 31 32 37 2E 30 2E 30 2E 31 3A 32 34 30 37 35 4E 75 6D 65 65 65 65 65 65 65 65 65 65 65 65 64 65 5F 73 65 67 61 63 73 74 72 69 6B 65 4E 75 6D 65 65 65 65 65 65 65 65 32 32 31 34 33 32 2F 64 77 01 


No any separator:((
Last edited on
sendto doesn't take a C string, strcpy does.
It should be sendto(sockfd,s.data(),s.size(), ...);
There is no need for the data array.
And like I said, don't use \0 in string literals, as these will be treated as C strings.
Write s+='\0'; instead.
Last edited on
Thx a lot Athar.
Is work fine now.

I appreciate that you are wasting your time with me, and u answer at my post at 1:01 am.

Thx again!
Topic archived. No new replies allowed.