[英]Use c string to send multiple null characters. ASCII Armoring
如何使用C字符串包含多个空字符\\ x00。
C's string functions (eg strlen()
, printf()
, etc) assume that the buffer will be null-terminated. C的字符串函数(例如
strlen()
, printf()
等)假设缓冲区将以空值终止。 If you have a buffer with multiple 0x00 characters, then you can't use any functions which treat 0x00 as a null character. 如果您有一个具有多个0x00字符的缓冲区,则不能使用任何将0x00视为空字符的函数。
So instead of using, eg, strcpy()
(or strncpy()
) you would use memcpy()
- to move the bytes of memory from one place to another, rather than relying on this null-terminated behavior. 因此,不使用例如
strcpy()
(或strncpy()
),而是使用memcpy()
- 将内存的字节从一个位置移动到另一个位置,而不是依赖于此以null结尾的行为。
AC string is only an array of chars terminated by a null character. AC字符串只是由空字符终止的字符数组。 However, if you treat it as an array, it can contain inner null characters:
但是,如果将其视为数组,则它可以包含内部空字符:
char data[4] = { 'x', '\0', 'y', '\0' };
You have to be careful, however, as most of the standard library functions will not work, as they expect an C string ending with the first null character. 但是,您必须要小心,因为大多数标准库函数都不起作用,因为它们期望C字符串以第一个空字符结尾。
For example, strlen(data)
will return 1 in the example above, as it stops after the first null character. 例如,
strlen(data)
将在上面的示例中返回1,因为它在第一个空字符后停止。
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's
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.