当前位置: 代码迷 >> 综合 >> int 转换成 byte[] (byte数组)
  详细解决方案

int 转换成 byte[] (byte数组)

热度:8   发布时间:2024-01-18 16:42:03.0

unsigned int x = 1234567;
 byte*  intBytes = new byte[4];

 //方法一
 memcpy(intBytes, &x, sizeof(unsigned int));
 unsigned int y1 = intBytes[3] *256*256*256 + intBytes[2] *256*256 + intBytes[1]* 256 + intBytes[0];

 //方法二
 intBytes[0]   =   (byte)   (x   >>   24);
 intBytes[1]   =   (byte)   (x   >>   16);
 intBytes[2]   =   (byte)   (x   >>   8);
 intBytes[3]   =   (byte)   (x   >>   0);

 unsigned int y2 = intBytes[0] *256*256*256 + intBytes[1] *256*256 + intBytes[2]* 256 + intBytes[3];

 

  相关解决方案