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