BYTE類型和int類型相互轉換


 1 byte不是一種新類型,在C++中byte被定義的是unsigned char類型;但在C#里面byte被定義的是unsigned int類型   
 2 //int轉byte 
 3 void  intToByte(int i,byte *bytes,int size = 4)   
 4 {  
 5      //byte[] bytes = new byte[4];  
 6     memset(bytes,0,sizeof(byte) *  size);  
 7     bytes[0] = (byte) (0xff & i);  
 8     bytes[1] = (byte) ((0xff00 & i) >> 8);  
 9     bytes[2] = (byte) ((0xff0000 & i) >> 16);  
10     bytes[3] = (byte) ((0xff000000 & i) >> 24);  
11     return ;  
12  }    
13 //byte轉int  
14  int bytesToInt(byte* bytes,int size = 4)   
15 {  
16     int addr = bytes[0] & 0xFF;  
17     addr |= ((bytes[1] << 8) & 0xFF00);  
18     addr |= ((bytes[2] << 16) & 0xFF0000);  
19     addr |= ((bytes[3] << 24) & 0xFF000000);  
20     return addr;  
21  }

本文為轉載,原地址:http://blog.csdn.net/qq61394323/article/details/44060613


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM