habi
habi

Reputation: 163

hexadecimal NULL inside the string

const char* sendbuf = "ABCD\x58\x34\x00\x84";

But the result is ABCDX4
X == \x58 , 4 == \x34

So my string got cut off in the middle. I understand it is because of \x00.

So how to have hexadecimal null \x00 in the middle of string ?

I am trying to send UDP packets.

Upvotes: 0

Views: 859

Answers (2)

Jarod42
Jarod42

Reputation: 217235

Issue with:

const char* sendbuf = "ABCD\x58\x34\x00\x84";

is that you lose size information.

You would keep that information, if you use C-array

const char sendbuf[/*8+1*/] = "ABCD\x58\x34\x00\x84";

or std::string literals:

using namespace std::literals;
const auto sendbuf = "ABCD\x58\x34\x00\x84"s; // std::string with size 8

Then you can display/send your buffer:

auto s = "ABCD\x58\x34\x00\x84"s;
std::cout << s;
sendUPD(/*..*/, s.c_str(), s.size());

char cs[] = "ABCD\x58\x34\x00\x84"s;
for (auto c : cs) { std::cout << c; } // "print" middle \0 and final one 
sendUPD(/*..*/, cs, sizeof(cs) - 1);

Upvotes: 0

habi
habi

Reputation: 163

the problems was not with the const char * sendbuf

It was with another function which

send(ConnectSocket, sendbuf, sizeof(sendbuf), 0);

Also i replaced const char sendbuf [];

Upvotes: 1

Related Questions