C++int到字节数组
C++ int to byte array
我的java代码中有这个方法,它为给定的int:返回字节数组
private static byte[] intToBytes(int paramInt)
{
byte[] arrayOfByte = new byte[4];
ByteBuffer localByteBuffer = ByteBuffer.allocate(4);
localByteBuffer.putInt(paramInt);
for (int i = 0; i < 4; i++)
arrayOfByte[(3 - i)] = localByteBuffer.array()[i];
return arrayOfByte;
}
有人能告诉我如何将该方法转换为C++吗?
您不需要一个完整的函数;一个简单的演员阵容就足够了:
int x;
static_cast<char*>(static_cast<void*>(&x));
C++中的任何对象都可以重新解释为字节数组。如果您想将字节复制到一个单独的数组中,可以使用std::copy
:
int x;
char bytes[sizeof x];
std::copy(static_cast<const char*>(static_cast<const void*>(&x)),
static_cast<const char*>(static_cast<const void*>(&x)) + sizeof x,
bytes);
这两种方法都不考虑字节排序,但由于您可以将int
重新解释为字节数组,因此您自己执行任何必要的修改都很简单。
使用std::vector<unsigned char>
:
#include <vector>
using namespace std;
vector<unsigned char> intToBytes(int paramInt)
{
vector<unsigned char> arrayOfByte(4);
for (int i = 0; i < 4; i++)
arrayOfByte[3 - i] = (paramInt >> (i * 8));
return arrayOfByte;
}
您可以通过安定和移位操作获得单个字节:
byte1 = nint & 0x000000ff
byte2 = (nint & 0x0000ff00) >> 8
byte3 = (nint & 0x00ff0000) >> 16
byte4 = (nint & 0xff000000) >> 24
我使用的另一种有用的方法是联合:
union byteint
{
byte b[sizeof int];
int i;
};
byteint bi;
bi.i = 1337;
for(int i = 0; i<4;i++)
destination[i] = bi.b[i];
这将使字节数组和整数"重叠"(共享同一内存(。只要字节数组与类型大小相同(否则其中一个字段不会受到另一个字段的影响(,就可以对所有类型执行此操作。当您必须在整数操作和字节操作/复制之间切换时,将它们作为一个对象也很方便。
std::vector<unsigned char> intToBytes(int value)
{
std::vector<unsigned char> result;
result.push_back(value >> 24);
result.push_back(value >> 16);
result.push_back(value >> 8);
result.push_back(value );
return result;
}
int(或任何其他数据类型(已作为字节存储在内存中。那么,为什么不直接复制内存呢?
memcpy(arrayOfByte, &x, sizeof x);
一个简单优雅的单行,也可以与任何其他数据类型一起使用。
如果需要反转字节,可以使用std::reverse
memcpy(arrayOfByte, &x, sizeof x);
std::reverse(arrayOfByte, arrayOfByte + sizeof x);
或者更好的是,只需反向复制字节,就可以从开始
BYTE* p = (BYTE*) &x;
std::reverse_copy(p, p + sizeof x, arrayOfByte);
如果你根本不想复制数据,只想让它的字节表示
BYTE* bytes = (BYTE*) &x;
Int到字节,反之亦然
unsigned char bytes[4];
unsigned long n = 1024;
bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8) & 0xFF;
bytes[3] = n & 0xFF;
printf("%x %x %x %xn", bytes[0], bytes[1], bytes[2], bytes[3]);
int num = 0;
for(int i = 0; i < 4; i++)
{
num <<= 8;
num |= bytes[i];
}
printf("number %d",num);
我知道这个问题已经有了答案,但我会给出这个问题的解决方案。我正在使用模板函数和整数约束。
这是我的解决方案:
#include <type_traits>
#include <vector>
template <typename T,
typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
std::vector<uint8_t> splitValueToBytes(T const& value)
{
std::vector<uint8_t> bytes;
for (size_t i = 0; i < sizeof(value); i++)
{
uint8_t byte = value >> (i * 8);
bytes.insert(bytes.begin(), byte);
}
return bytes;
}
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
:D
我希望我的帮助
template <typename t_int>
std::array<uint8_t, sizeof (t_int)> int2array(t_int p_value) {
static const uint8_t _size_of (static_cast<uint8_t>(sizeof (t_int)));
typedef std::array<uint8_t, _size_of> buffer;
static const std::array<uint8_t, 8> _shifters = {8*0, 8*1, 8*2, 8*3, 8*4, 8*5, 8*6, 8*7};
buffer _res;
for (uint8_t _i=0; _i < _size_of; ++_i) {
_res[_i] = static_cast<uint8_t>((p_value >> _shifters[_i]));
}
return _res;
}
相关文章:
- 保存/加载大量短数组到二进制文件
- 字符数组到无符号字符 *
- Int 数组到C++容器
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- C++字节数组到整数
- 为什么数组到指针的输出不相关
- win32 字节数组到列表框中
- 字符数组到十六进制字符串的转换 - 意外输出
- 从常量字符数组到字符数组的隐式转换
- 如何在 c++ 中读取/写入无符号数组到 ifstream/ostream?
- 从C++(非托管代码)检索数组到 C 尖锐形式(托管)
- 从结构的 C 数组到带有 ctypes 的 NumPy 数组的高效转换
- NV21 字节数组到 array2d<rgb_pixel>
- HBitmap字节数组到php
- 我的字节数组到十六进制字符串转换器有什么问题,或者为什么它在写入文件时会在某个位置后剪切符号?
- 字节数组到变量
- 在Matlab中编写一个固定长度的字节数组到二进制文件
- 转换uint8字节数组到任何WPF渲染对象
- 字节数组到C++中的int