linux c++ 如何实现unsigned char数组转换为char数组
时间: 2024-01-09 12:04:07 浏览: 86
如何把char str,或unsigned char str转换成CString
4星 · 用户满意度95%
可以使用C++中的memcpy函数来实现unsigned char数组到char数组的转换。具体实现可参考下面的示例代码:
```c++
#include <iostream>
#include <cstring>
int main()
{
// 定义unsigned char数组
unsigned char unsignedCharArray[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F};
// 计算unsigned char数组的长度
size_t unsignedCharArrayLength = sizeof(unsignedCharArray) / sizeof(unsignedCharArray[0]);
// 定义char数组并初始化为0
char charArray[unsignedCharArrayLength + 1] = {0};
// 将unsigned char数组数据复制到char数组
memcpy(charArray, unsignedCharArray, unsignedCharArrayLength);
// 输出char数组
std::cout << "charArray: " << charArray << std::endl;
return 0;
}
```
输出结果为:
```
charArray: Hello
```
阅读全文