Use string c to send multiple null characters. ASCII Armoring

How to use C string to contain multiple null characters \ x00.

+3


source to share


3 answers


String Functions C (eg strlen()

, printf()

etc.) assume that the buffer will end with zero. If you have a buffer with multiple 0x00 characters, then you cannot use any functions that treat 0x00 as a null character.



So instead of using, for example, strcpy()

(or strncpy()

), you should use memcpy()

- to move bytes of memory from one location to another, instead of relying on this null-terminated behavior.

+9


source


String C is just an array of null-terminated characters. However, if you consider it as an array, it can contain internal null characters:

char data[4] = { 'x', '\0', 'y', '\0' };

      



However, you have to be careful, as most of the standard library functions will not work as they expect the C string to end with the first null character.

For example, strlen(data)

will return 1 in the example above, since it stops after the first null character.

+3


source


All you have to do is something like

char* data = (char*)malloc( 8 ) ; // allocate space for 8 bytes
memset( data, 0, 8 ) ; // set all 8 bytes to 0 

      

+2


source







All Articles