Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将u_char转换为char_C_Char - Fatal编程技术网

将u_char转换为char

将u_char转换为char,c,char,C,Char,我有一个函数,它在u\u char数组中返回一个值。有没有办法将其转换为char字符串 以下是我所拥有的: u_char *buf = NULL; size_t out_len = 0; out\u len返回更新的“买我的”函数的字符数。 另外,我不确定buf是否以null结尾。 我试过printf(“%s”,buf_len,buf)

我有一个函数,它在
u\u char
数组中返回一个值。有没有办法将其转换为
char
字符串

以下是我所拥有的:

u_char *buf = NULL;
size_t out_len = 0;
out\u len
返回更新的“买我的”函数的字符数。 另外,我不确定
buf
是否以null结尾。
我试过
printf(“%s”,buf_len,buf)据我所知,
printf(“%s”,buf_len,buf)将查找以NULL结尾的字符串

一种解决方案是使用
strncpy
将所有字符复制到一个数组中

strncpy(my_array, (const char *)buf, buf_len);
确保
my_array
应该足够大,可以容纳
buf
和一个空字符。

sprintf()可以(或memcpy/strcpy),但这取决于实现。此外,请注意,字符数组中的项目数并不等于字符数(因为字符的编码和区域性特定定义)。