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

posted @ 2017-11-10 09:31  冥天笑  阅读(4416)  评论(0编辑  收藏  举报