简体   繁体   English

使用c string发送多个空字符。 ASCII装甲

[英]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.

相关问题 检查字符串中的字符是字母,数字还是特殊字符。 在C - checking if the characters in a string are either alphabets, numbers, or special characters. in c 在C中的多列中打印ASCII字符 - Print ASCII characters in multiple columns in C (适用于初学者):输入字符数组,无特定大小。 使用:字符数组。 创建一个实现间隔的条件。 (在C中) - (For beginners): Input Array of Characters w/ no specific size. Use of: Array of Characters. Create a condition that implements an interval. (in C) 将引号中的字符串转换为C中的ascii2字节数组 - Transferring a string of characters in quotes to an array of ascii2 bytes in C 如何将字符串字符的 ASCII 值解释为 C 中的字符? - How do I interpret ASCII values of characters of a string as chars in C? 使用ascii值将char数组中的多个ascii字符转换为单个int - c / c ++ - conversion of multiple ascii characters in a char array to single int using their ascii values — c/c++ 在一行C上将字符串的各个字符转换为ASCII值 - Converting individual characters of a string into ASCII values on one line, C 使用C中包含多个Null字符的字符串 - Working with strings in C that contain multiple Null characters 在C中的数组中存储ASCII字符 - Storing ASCII characters in an array in C 在C中将int转换为ASCII字符 - Convert int to ASCII characters in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM