c++ 基本类型与byte 数组互相转换
前言
由于根据编译器的不同 有部分基础数据类型字节数不一致
本文档 基础类型位数如下
bool 字节数: 1
char 字节数: 1
short 字节数: 2
int 字节数: 4
long 字节数: 4
long long 字节数: 8
float 字节数: 4
double 字节数: 8
long double 字节数: 12
说明
传统C方式 位操作: 使用一个unsigned int变量来作为位容器。
强制指针类型转换。
由于C++ 中没有Bety 类型 所以 typedef unsigned char byte;
INT 与 byte数组互转
/**
* convert int type to byte array
*/
static void intToByte(int i, byte* bytes);
/**
* convert byte array to int type
*/
static int bytesToInt(byte* bytes);
void ByteUtil::intToByte(int i, byte* bytes)
{
size_t length = sizeof(int);
// 初始化数组
memset(bytes, 0, sizeof(byte) * length);
bytes[0] = (byte)(0xff & i);
bytes[1] = (byte)((0xff00 & i) >> 8);
bytes[2] = (byte)((0xff0000 & i) >> 16);
bytes[3] = (byte)((0xff000000 & i) >> 24);
return;
}
int ByteUtil::bytesToInt(byte* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
int addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
long与 byte数组互转
/**
* convert long type to byte array
*/
static void longToByte( long i, byte* bytes);
/**
* convert byte array to long type
*/
static long bytesToLong(byte* bytes);
void ByteUtil::longToByte(long i, byte *bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
size_t length = sizeof(long);
memset(bytes, 0, sizeof(byte) * length);
bytes[0] = (byte)(0xff & i);
bytes[1] = (byte)((0xff00 & i) >> 8);
bytes[2] = (byte)((0xff0000 & i) >> 16);
bytes[3] = (byte)((0xff000000 & i) >> 24);
}
long ByteUtil::bytesToLong(byte *bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
long addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
LONG LONG 与 byte数组互转
/**
* convert long long type to byte array
*/
static void longLongToByte( long long i, byte* bytes);
/**
* convert byte array to long long type
*/
static long long bytesToLongLong(byte* bytes);
void ByteUtil::longLongToByte(long long i, byte* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
size_t length = sizeof(long long);
memset(bytes, 0, sizeof(byte) * length);
bytes[0] = (byte)(0xff & i);
bytes[1] = (byte)((0xff00 & i) >> 8);
bytes[2] = (byte)((0xff0000 & i) >> 16);
bytes[3] = (byte)((0xff000000 & i) >> 24);
bytes[4] = (byte)((0xff00000000 & i) >> 32);
bytes[5] = (byte)((0xff0000000000 & i) >> 40);
bytes[6] = (byte)((0xff000000000000 & i) >> 48);
bytes[7] = (byte)((0xff00000000000000 & i) >> 56);
}
long long ByteUtil::bytesToLongLong(byte* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
long long addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
addr |= ((((long long) bytes[4]) << 32) & 0xFF00000000);
addr |= ((((long long) bytes[5]) << 40) & 0xFF0000000000);
addr |= ((((long long) bytes[6]) << 48) & 0xFF000000000000);
addr |= ((((long long) bytes[7]) << 56) & 0xFF00000000000000);
return addr;
}
short 与 byte数组互转
/**
* convert Short type to byte array
*/
static void shortToByte(short i, byte* bytes);
/**
* convert byte array to Short type
*/
static short bytesToShort(byte* bytes);
void ByteUtil::shortToByte(short i, byte* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
size_t length = sizeof(short);
//byte[] bytes = new byte[4];
memset(bytes, 0, sizeof(byte) * length);
bytes[0] = (byte)(0xff & i);
bytes[1] = (byte)((0xff00 & i) >> 8);
return;
}
short ByteUtil::bytesToShort(byte* bytes)
{
// 位操作时 使用一个unsigned int变量来作为位容器。
short addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
return addr;
}
double 与 byte数组互转
/**
* convert byte array to double type
*/
static double bytesToDouble(byte bytes[]);
/**
* c++ double type length is 8 byte
* convert double type to byte array
*/
static void doubleTobytes(double data, byte bytes[]);
double ByteUtil::bytesToDouble(byte bytes[])
{
// 位操作时 使用一个unsigned int变量来作为位容器。
double data = *((double*)bytes);
return data;
}
void ByteUtil::doubleTobytes(double data, byte bytes[])
{
// 位操作时 使用一个unsigned int变量来作为位容器。
int i;
size_t length = sizeof(double);
char* p = (char*)&data;
for (i = 0; i < length; i++)
{
bytes[i] = *p++;
}
}
long double 与 byte数组互转
/**
* convert byte array to double type
*/
static long double bytesToLongDouble(byte bytes[]);
/**
* c++ double type length is 8 byte
* convert long double type to byte array
*/
static void longDoubleTobytes(long double data, byte bytes[]);
long double ByteUtil::bytesToLongDouble(byte bytes[])
{
// 位操作时 使用一个unsigned int变量来作为位容器。
long double data = *((long double *)bytes);
return data;
}
void ByteUtil::longDoubleTobytes(long double data, byte bytes[])
{
// 位操作时 使用一个unsigned int变量来作为位容器。
int i;
size_t length = sizeof(long double);
char* p = (char*)&data;
for (i = 0; i < length; i++)
{
bytes[i] = *p++;
}
}
float 与 byte数组互转
/**
* convert byte array to float type
* C语言可用
*/
static float bytesToFloat(byte bytes[]);
/**
* convert float type to byte array
*/
static void floatTobytes(float data, byte bytes[]);
float RKByteUtil::bytesToFloat(byte bytes[])
{
// 位操作时 使用一个unsigned int变量来作为位容器。
return *((float*)bytes);
}
void RKByteUtil::floatTobytes(float data, byte bytes[])
{
// 位操作时 使用一个unsigned int变量来作为位容器。
int i;
size_t length = sizeof(float);
byte *pdata = (byte*)&data; //把float类型的指针强制转换为unsigned char型
for (i = 0; i < length; i++)
{
bytes[i] = *pdata++;//把相应地址中的数据保存到unsigned char数组中
}
return;
}
bool与 byte数组互转
/**
* convert byte array to bool type
* C语言可用
*/
static bool bytesToBool(byte bytes[]);
/**
*
* convert bool type to byte array
*/
static void boolTobytes(bool data, byte bytes[]);
bool ByteUtil::bytesToBool(byte bytes[])
{
return (bytes[0] & 0x01) == 1 ? true : false;
}
void ByteUtil::boolTobytes(bool data, byte bytes[])
{
if (data) {
bytes[0] = (byte)(0x01 & 1);
}
else {
bytes[0] = (byte)(0x01 & 0);
}
}