程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C語言的無符號數據類型轉換

C語言的無符號數據類型轉換

編輯:關於C語言

從網絡中讀取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;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved