C++ 中byte[] 类型到long类型的转换
最近因为需要写一个ActiveX,需要在C++中有一个对应C#中byte[] 类型到long类型的转换, byte[] 和long类型分别对于C++中的unsign char[] 和long long 类型,这个我一开始的时候犯了个错误,用的是long类型,所以结果一直不对,查了资料后才知道对应C#中long应该是C++中的long long .
Type Name | Bytes | Other Names | Range of Values |
---|---|---|---|
char | 1 | signed char | –128 to 127 |
unsigned char | 1 | none | 0 to 255 |
long | 4 | long int, signed long int | –2,147,483,648 to 2,147,483,647 |
long long | 8 | none (but equivalent to __int64) | –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
转换的函数如下:
long long byteToLong(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3,unsigned char b4, unsigned char b5, unsigned char b6, unsigned char b7)
{
unsigned char aChar[8]= {b0,b1,b2,b3,b4,b5,b6,b7};
return *(((long long*)aChar));
}
{
unsigned char aChar[8]= {b0,b1,b2,b3,b4,b5,b6,b7};
return *(((long long*)aChar));
}