C++ – 字节数组byte[]或者unsigned char[]与long long的相互转换
设定long long型长度为8。
1 long long转字节数组
long long型转字节数组byte[]或者unsigned char[]
void LongLongToBytes(long long value, unsigned char* bytes)
{
size_t length = sizeof(long long);
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);
bytes[4] = (unsigned char)((0xff00000000 & value) >> 32);
bytes[5] = (unsigned char)((0xff0000000000 & value) >> 40);
bytes[6] = (unsigned char)((0xff000000000000 & value) >> 48);
bytes[7] = (unsigned char)((0xff00000000000000 & value) >> 56);
}
2 字节数组转long long
字节数组byte[]或者unsigned char[]转long long型
long long BytesToLongLong(unsigned char* bytes)
{
long long value = bytes[0] & 0xFF;
value |= ((bytes[1] << 8) & 0xFF00);
value |= ((bytes[2] << 16) & 0xFF0000);
value |= ((bytes[3] << 24) & 0xFF000000);
value |= ((((long long)bytes[4]) << 32) & 0xFF00000000);
value |= ((((long long)bytes[5]) << 40) & 0xFF0000000000);
value |= ((((long long)bytes[6]) << 48) & 0xFF000000000000);
value |= ((((long long)bytes[7]) << 56) & 0xFF00000000000000);
return value;
}
3 使用示例
#include <iostream>
void LongLongToBytes(long long value, unsigned char* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
size_t length = sizeof(long long);
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);
bytes[4] = (unsigned char)((0xff00000000 & value) >> 32);
bytes[5] = (unsigned char)((0xff0000000000 & value) >> 40);
bytes[6] = (unsigned char)((0xff000000000000 & value) >> 48);
bytes[7] = (unsigned char)((0xff00000000000000 & value) >> 56);
}
long long BytesToLongLong(unsigned char* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
long long value = bytes[0] & 0xFF;
value |= ((bytes[1] << 8) & 0xFF00);
value |= ((bytes[2] << 16) & 0xFF0000);
value |= ((bytes[3] << 24) & 0xFF000000);
value |= ((((long long)bytes[4]) << 32) & 0xFF00000000);
value |= ((((long long)bytes[5]) << 40) & 0xFF0000000000);
value |= ((((long long)bytes[6]) << 48) & 0xFF000000000000);
value |= ((((long long)bytes[7]) << 56) & 0xFF00000000000000);
return value;
}
int main()
{
unsigned char longlongByteArray[8];
long long a = 10;
LongLongToBytes(a, longlongByteArray);
std::cout << BytesToLongLong(longlongByteArray) << std::endl;
return 0;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 字节数组byte[]或者unsigned char[]与long long的相互转换
原文链接:https://www.stubbornhuang.com/2029/
发布于:2022年03月12日 23:08:42
修改于:2023年06月26日 20:29:01
当前分类随机文章推荐
- C++ - linux编译C++代码出现error: use of deleted function std::atomic
::atomic(const std::atomic 阅读3690次,点赞0次&) - C++ - Windows/Linux跨平台获取本机CPU核心数 阅读490次,点赞0次
- C++ - 最简单的将文本文件的内容一次性读取到std::string的方法 阅读5642次,点赞4次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读3266次,点赞0次 - C++ - 使用Crypto++/CryptoPP加解密库对字符串或者文件进行AES256加密 阅读4570次,点赞1次
- C++ - std::string字符串格式化方法总结 阅读1956次,点赞0次
- C++ - 使用Spout2将视频流发送到OBS 阅读216次,点赞0次
- C++ - Asio和Boost.Asio的区别 阅读290次,点赞0次
- C++11 - std::chrono - 使用std::chrono::duration_cast进行时间转换,hours/minutes/seconds/milliseconds/microseconds相互转换,以及自定义duration进行转换 阅读2538次,点赞0次
- C++ - std::map - 存储动态指针时正确释放内存 阅读4635次,点赞1次
全站随机文章推荐
- Alphapose - Windows下Alphapose(Pytorch 1.1+)版本2021最新环境配置步骤以及踩坑说明 阅读5753次,点赞1次
- UnrealEngine4 - 将FTexture2DRHIRef保存为图片 阅读3923次,点赞0次
- 资源分享 - The NURBS Book (2nd,Les Pieg) 英文版PDF下载 阅读6812次,点赞3次
- Pytorch - torch.nn.Module的parameters()和named_parameters() 阅读799次,点赞0次
- PaddlePaddle - 使用Paddle2Onnx将Paddle模型导出为onnx模型 阅读198次,点赞0次
- 资源分享 - Fundamentals of Computer Graphics, Third Edition高清英文PDF下载 阅读4607次,点赞1次
- Modern OpenGL从零开始 - 从茫茫多的OpenGL第三方库讲起 阅读3850次,点赞1次
- 资源分享 - Jim Blinn's Corner - Notation, Notation, Notation 英文高清PDF下载 阅读2365次,点赞0次
- TensorRT - 使用TensorRT C++ SDK部署模型时推理时间波动不稳定或者推理速度越来越慢的问题 阅读255次,点赞0次
- Python - 类对象/列表/元祖/字典判空的方法 阅读2576次,点赞0次
感谢大佬的解答,收获很多。
有一个疑问,在BytesToLongLong方法中为什么要进行 与0xFF操作呢?
比如((bytes[1] << 8) & 0xFF00),即使不&FF00,bytes[1]左移了8位后低8位依然是0。我感觉再与上FF00有点多此一举。