参考:
- 一文穷尽所有C++的数据类型
- C++ 类型转换(内置类型,string、char*、const char*等)
- c++基本类型与 byte数组互转(***)
- C++中将char数组转换成double类型的方法(***)
- C语言中的类型转换(***)
- C语言中的整数(short,int,long)
- 数组万能复制——memcpy()
- 使用memcpy 复制unsigned int 型的数据
- [c++]数据类型之间的转换(string与基本类型int,float,double,long long等)(***)
- c/c++ bytes与int,short,long,float,double相互转换(**)
正文: 方法一: 采用函数来进行转化 c++ 中没有byte类型,先要定义byte
typedef unsigned char byte;
转化函数:
void shortToByte(short i, byte* bytes)
{
size_t length = sizeof(short);
memset(bytes, 0, sizeof(byte) * length);
bytes[0] = (byte)(0xff & i);
bytes[1] = (byte)((0xff00 & i) >> 8);
}
short bytesToShort(byte* bytes)
{
short addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
return addr;
}
void intToBytes(int i, byte* bytes) {
int size = sizeof(int);
memset(bytes,0,sizeof(byte) * size);
bytes[0] = (byte) (0xff & i);
bytes[1] = (byte) ((0xff00 & i) >> 8);
bytes[2] = (byte) ((0xff0000 & i) >> 16);
bytes[3] = (byte) ((0xff000000 & i) >> 24);
}
int bytesToInt(byte* bytes) {
int iRetVal = bytes[0] & 0xFF;
iRetVal |= ((bytes[1] << 8) & 0xFF00);
iRetVal |= ((bytes[2] << 16) & 0xFF0000);
iRetVal |= ((bytes[3] << 24) & 0xFF000000);
return iRetVal;
}
float bytesToFloat(byte bytes[])
{
return *((float*)bytes);
}
void floatTobytes(float data, byte bytes[])
{
int i;
size_t length = sizeof(float);
byte *pdata = (byte*)&data;
for (i = 0; i < length; i++)
{
bytes[i] = *pdata++;
}
return;
}
主函数: 先复习一下基础知识:
char 1 byte -128 ~ 127
unsigned char 1 byte 0 ~ 255
signed char 1 byte -128 ~ 127
short int 2 byte -32768 ~ 32767
int main(){
std::cout << "...short <==> byte... " << std::endl;
short i = 124;
byte ch[2];
shortToByte(i,ch);
short b = bytesToShort(ch);
std::cout << (short)ch[0] << std::endl;
std::cout << b << std::endl;
std::cout << "...int <==> byte... " << std::endl;
int i1 = 40000;
byte ch1[4];
intToBytes(i1,ch1);
int i12 = bytesToInt(ch1);
std::cout << (int)ch1[0] << std::endl;
std::cout << i12 << std::endl;
std::cout << "...float <==> byte... " << std::endl;
float f1 = 9600.17;
byte ch2[4];
floatTobytes(f1,ch2);
std::cout << "sizeof(ch2): " << sizeof(ch2) << std::endl;
float f12 = bytesToFloat(ch2);
std::cout << (float)ch2[0] << std::endl;
std::cout << f12 << std::endl;
return 0;
}
结果输出:
...short <==> byte...
124
124
...int <==> byte...
64
40000
...float <==> byte...
sizeof(ch2): 4
174
9600.17
在 short 和 Byte互转过程中, if 0 <= short i <= 255, then ** (short)ch[0]** 等于**short i
复制到byte数组时:
byte sendData[16] = { 0 };
memcpy(sendData,ch,2);
memcpy(sendData+2,ch1,4);
memcpy(sendData+6,ch2,4);
short data1 = bytesToShort(sendData);
int data2 = bytesToInt(sendData+2);
float data3 = bytesToFloat(sendData+6);
std::cout << data1 << " " << data2 << " " << data3 << std::endl;
其结果为:
124 40000 9600.17
方法二: 利用联合体union的特性转化 先定义联合体 union short2byte
union short2byte
{
unsigned char byte[2];
short value;
};
在主函数中,接着上面的byte 数组sendData
short2byte x;
x.value = 32767;
memcpy(sendData+10,x.byte,2);
可以这样转化
short data4 = bytesToShort(sendData+10);
std::cout << data4<< std::endl;
也可以这样转化
short2byte x1;
memcpy(x1.byte,sendData+10,2);
std::cout << x1.value << std::endl;
打印结果:
data4: 32767
x1.value: 32767
|