当前位置:天才代写 > tutorial > C语言/C++ 教程 > C语言的无标记数据范例转换

C语言的无标记数据范例转换

2017-11-02 08:00 星期四 所属: C语言/C++ 教程 浏览:45

从网络中读取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;

 

    关键字:


天才代写-代写联系方式