一个字节为8位二进制位
1 int转字节数组byte[]
C++中,字节数组byte通常用unsigned char表示,所以int转换为字节数组本质上是将int转换为unsigned char数组。int一般为4个字节,那么就为32位二进制位表示。
代码如下:
void IntToByte(int value, unsigned char* bytes)
{
size_t length = sizeof(int);
memset(bytes, 0, sizeof(unsigned char) * length);
bytes[0] = (unsigned char)(0xff & value);
bytes[1] = (unsigned char)((0xff00 & value) >> 8);
bytes[2] = (unsigned char)((0xff0000 & value) >> 16);
bytes[3] = (unsigned char)((0xff000000 & value) >> 24);
}
2 字节数组byte[]转int
代码如下:
int ByteToInt(unsigned char* byteArray)
{
int value = byteArray[0] & 0xFF;
value |= ((byteArray[1] << 8) & 0xFF00);
value |= ((byteArray[2] << 16) & 0xFF0000);
value |= ((byteArray[3] << 24) & 0xFF000000);
return value;
}
3 使用场景
例如在有些情况下的通信协议,举一个简单的栗子,在socket通信下,我们通常会想要知道本次接收的数据的长度有多大,那么这个时候就可以在要发送数据块的前面使用4个字节标志数据块的大小,而这时候就需要将int转换为字节数组。
4 使用示例
#include <iostream>
void IntToByte(int value, unsigned char* bytes)
{
size_t length = sizeof(int);
memset(bytes, 0, sizeof(unsigned char) * length);
bytes[0] = (unsigned char)(0xff & value);
bytes[1] = (unsigned char)((0xff00 & value) >> 8);
bytes[2] = (unsigned char)((0xff0000 & value) >> 16);
bytes[3] = (unsigned char)((0xff000000 & value) >> 24);
}
int ByteToInt(unsigned char* byteArray)
{
int value = byteArray[0] & 0xFF;
value |= ((byteArray[1] << 8) & 0xFF00);
value |= ((byteArray[2] << 16) & 0xFF0000);
value |= ((byteArray[3] << 24) & 0xFF000000);
return value;
}
int main()
{
unsigned char intByteArray[4];
int a = 10;
IntToByte(a, intByteArray);
std::cout << ByteToInt(intByteArray) << std::endl;
return 0;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 字节数组byte[]或者unsigned char[]与int的相互转换
原文链接:https://www.stubbornhuang.com/960/
发布于:2020年11月10日 10:26:20
修改于:2023年06月26日 22:08:07
当前分类随机文章推荐
- C++ - std::string字符串格式化方法总结 阅读1956次,点赞0次
- C++ - 获取std::vector中的最小值、最大值以及对应的索引 阅读484次,点赞0次
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读5816次,点赞2次
- Centos7 编译C++项目错误解决 : terminate called after throwing an instance of 'std::regex_error' 阅读2819次,点赞1次
- C++ - 令人迷惑的const关键字 阅读93次,点赞0次
- C++ - 随机洗牌算法,std::random_shuffle和std::shuffle 阅读2256次,点赞2次
- C++ - 使用正则判断字符串是否全是中文 阅读1664次,点赞0次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读3266次,点赞0次 - CMake - 设置Debug或者Release编译模式 阅读3978次,点赞0次
- C++ - Windows系统使用C++切换音频默认输出设备 阅读75次,点赞0次
全站随机文章推荐
- 资源分享 - 深度学习的数学 (涌井良幸 涌井贞美著) 高清PDF下载 阅读5659次,点赞3次
- C++ - Windows系统使用C++切换音频默认输出设备 阅读75次,点赞0次
- 资源分享 - 游戏物理引擎开发, Game Physics Engine Development 中文版PDF下载 阅读2523次,点赞0次
- 我的开源项目 - 使用OnnxRuntime在CPU端部署RTMPose玩转实时2D姿态估计 阅读839次,点赞1次
- 怎么样正确的读科研论文? 阅读432次,点赞0次
- 计算几何 - 求解三维平面法向量 阅读159次,点赞0次
- 资源分享 - Ray Tracing Gems II - Next Generation Real-Time Rendering with DXR, Vulkan, and OptiX-Apress 英文高清PDF下载 阅读2332次,点赞0次
- 资源分享 - GPU Pro 7 - Advanced Rendering Techniques 英文高清PDF下载 阅读2990次,点赞0次
- 资源分享 - Essential Mathematics for Games and Interactive Applications(Second Edition) 英文高清PDF下载 阅读2209次,点赞0次
- 资源分享 - Guide to Computational Geometry Processing Foundations, Algorithms, and Methods英文高清PDF下载 阅读2026次,点赞0次
评论
169