| C语言的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short |
| 来源:
发布时间:2008-03-06 发布人:
浏览:
人次
字体:
[大
中
小]
|
|
从网络中读取C的无符号int,short,byte字节数组,相应转换成java的long,char,short
short anUnsignedByte = 0; char anUnsignedShort = 0; long anUnsignedInt = 0; int firstByte = 0; int secondByte = 0; int thirdByte = 0; int fourthByte = 0;
byte buf[] = getNetData();//如:读网络字节数据 int index = 0; firstByte = (0x000000FF & ((int)buf[index])) index++; anUnsignedByte = (short)firstByte;
firstByte = (0x000000FF & ((int)buf[index])) secondByte = (0x000000FF & ((int)buf[index+1])) index = index+2; anUnsignedShort = (char) (firstByte << 8 | secondByte);
firstByte = (0x000000FF & ((int)buf[index])) secondByte = (0x000000FF & ((int)buf[index+1])) thirdByte = (0x000000FF & ((int)buf[index+2])) fourthByte = (0x000000FF & ((int)buf[index+3])) index = index+4; anUnsignedInt = ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
| |
| |
|
|
|
|
| §最新评论:(评论内容只代表网友观点,与本站立场无关!) | |
|
|
|
|
| 注意:请勿在本站发布政治话题、色情及违反法律的内容。 |
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。 |