将一个int数据转化为一个unsigned char数组的函数代码,并引用
时间: 2024-04-30 17:24:12 浏览: 74
unsigned char数组转成string 用16进制表示
下面是将一个int数据转化为一个unsigned char数组的函数代码:
```c++
void intToByteArray(int value, unsigned char* byteArray) {
byteArray[0] = (value >> 24) & 0xFF;
byteArray[1] = (value >> 16) & 0xFF;
byteArray[2] = (value >> 8) & 0xFF;
byteArray[3] = value & 0xFF;
}
```
使用方法:
```c++
int value = 12345;
unsigned char byteArray[4];
intToByteArray(value, byteArray);
```
引用:
```c++
#include <iostream>
void intToByteArray(int value, unsigned char* byteArray) {
byteArray[0] = (value >> 24) & 0xFF;
byteArray[1] = (value >> 16) & 0xFF;
byteArray[2] = (value >> 8) & 0xFF;
byteArray[3] = value & 0xFF;
}
int main() {
int value = 12345;
unsigned char byteArray[4];
intToByteArray(value, byteArray);
std::cout << "Int value: " << value << std::endl;
std::cout << "Byte array: ";
for (int i = 0; i < 4; i++) {
std::cout << (int)byteArray[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
阅读全文