Putting 0x00 into a char array without it terminating (C++)

bignateyk

Lifer
Apr 22, 2002
11,288
7
0
I am trying to fill a char array with some hex values, and I need one of the values to be 0x00.

The problem is that C++ has 0x00 as the terminating character, so anything I put after 0x00 it will just ignore.

For example, I want to fill a character array with something like:

0xC0 0x00 0x01 0xBB 0xFC 0xFD

But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

My brain is fried for the day, so I might just be missing something obvious, so feel free to point out my stupidity.
 

leftyman

Diamond Member
Sep 15, 2000
7,073
3
81
:cookie: I dont have a clue what you're on about, so I think I would be the stupid one here :)
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
This needs to go in the programming forum :|

Oh, wait.
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: bignateyk
But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

What is "it"?
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
can't you just escape it with a backslash?
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
The terminating null character only has significance if you're trying to treat the char array as a string. You can store any byte you want in the char array, but don't try to print it out as a string. Make sense?
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: Evadman
can't you just escape it with a backslash?

It sounds like you're thinking about the char[] as a string, instead of as an array of type char, which would result in it being truncated at a null.

And now that I think about it, I think that's what his problem is too.
 

bignateyk

Lifer
Apr 22, 2002
11,288
7
0
Originally posted by: bersl2
Originally posted by: bignateyk
But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

What is "it"?


It, my best guess, would be the compiler. I fill the array with the values, and when I pass it somewhere, or send it to a function, it reads the 0x00 as the end of the array, and ignores the rest of the data I filled it with since 0x00 is a terminating character.
 

bignateyk

Lifer
Apr 22, 2002
11,288
7
0
Originally posted by: Descartes
The terminating null character only has significance if you're trying to treat the char array as a string. You can store any byte you want in the char array, but don't try to print it out as a string. Make sense?


I am transmitting the array over a UDP socket, and then printing out the data it receives.. maybe it is receiving it all, but just not printing it out.

I told you it was probably something stupid :)

Lemme go run a breakpoint and see if the whole array is there


edit: yep, that was the problem, im an idiot.
 

talyn00

Golden Member
Oct 18, 2003
1,666
0
0
Originally posted by: Descartes
The terminating null character only has significance if you're trying to treat the char array as a string. You can store any byte you want in the char array, but don't try to print it out as a string. Make sense?

bingo!
 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: bignateyk
Originally posted by: bersl2
Originally posted by: bignateyk
But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

What is "it"?


It, my best guess, would be the compiler. I fill the array with the values, and when I pass it somewhere, or send it to a function, it reads the 0x00 as the end of the array, and ignores the rest of the data I filled it with since 0x00 is a terminating character.

How are you filling the array with values, and what library functions are you ultimately sending it to?

and then printing out the data it receives

See, that would be treating the char[] as a string, which is not what you want.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: bignateyk
Originally posted by: bersl2
Originally posted by: bignateyk
But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

What is "it"?


It, my best guess, would be the compiler. I fill the array with the values, and when I pass it somewhere, or send it to a function, it reads the 0x00 as the end of the array, and ignores the rest of the data I filled it with since 0x00 is a terminating character.

The "end" of an array is precisely the size of the array / size of the type you put in it, and the compiler treats it as such. There's no special byte that says, "We're at the end" (ok, maybe some compilers do emit a special byte, but I'm not familiar with any). If you increment a pointer beyond the boudns of an array you'll have a wild pointer, and in many environments an exception. It doesn't care what byte you put in there. As others have said, and as I posted originally, only functions that process those arrays care. The most notable example is string functions, and they most definitely care about the terminating character.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: bignateyk
Originally posted by: Descartes
The terminating null character only has significance if you're trying to treat the char array as a string. You can store any byte you want in the char array, but don't try to print it out as a string. Make sense?


I am transmitting the array over a UDP socket, and then printing out the data it receives.. maybe it is receiving it all, but just not printing it out.

I told you it was probably something stupid :)

Lemme go run a breakpoint and see if the whole array is there


edit: yep, that was the problem, im an idiot.

Glad to hear it.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: bignateyk
Originally posted by: bersl2
Originally posted by: bignateyk
But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

What is "it"?


It, my best guess, would be the compiler. I fill the array with the values, and when I pass it somewhere, or send it to a function, it reads the 0x00 as the end of the array, and ignores the rest of the data I filled it with since 0x00 is a terminating character.

Why don't you transfer the length of the array as well, so not only do you know how much to read, you can also verify that you received all of the data?
 

sao123

Lifer
May 27, 2002
12,653
205
106
Originally posted by: bignateyk
I am trying to fill a char array with some hex values, and I need one of the values to be 0x00.

The problem is that C++ has 0x00 as the terminating character, so anything I put after 0x00 it will just ignore.

For example, I want to fill a character array with something like:

0xC0 0x00 0x01 0xBB 0xFC 0xFD

But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

My brain is fried for the day, so I might just be missing something obvious, so feel free to point out my stupidity.


Is this some type of buffer overflow crap? What exactly are you trying to do!!!!

 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: sao123
Originally posted by: bignateyk
I am trying to fill a char array with some hex values, and I need one of the values to be 0x00.

The problem is that C++ has 0x00 as the terminating character, so anything I put after 0x00 it will just ignore.

For example, I want to fill a character array with something like:

0xC0 0x00 0x01 0xBB 0xFC 0xFD

But when I do so, it just ignores everything after 0x00 because it thinks it is the terminating character.

My brain is fried for the day, so I might just be missing something obvious, so feel free to point out my stupidity.


Is this some type of buffer overflow crap? What exactly are you trying to do!!!!

Read the other posts!!!!

:D
 

OOBradm

Golden Member
May 21, 2001
1,730
1
76
usually, \n ends a line, but there is a way you can get it to output to the console
something like
cout<<"#%(*&@#$\n";

where you replace #%(*&@#$ with some character that tells the compiler to use it just as text and not as a end line

I'll bet you can use that same thing with your problem. However, i forget what it is and i cant find it anywhere