Develop
[java] int <-> byte[] 변환
freetome
2021. 9. 17. 14:33
// byte[] -> int
public int bytesToInt(byte[] b ){
int temp1 = (int) (b[3]) & 0xff;
int temp2 = ((int) (b[2]) & 0xff) << 8;
int temp3 = ((int) (b[1]) & 0xff) << 16;
int temp4 = ((int) (b[0]) & 0xff) << 24;
int temp = temp1 + temp2 + temp3 + temp4;
return temp;
}
//int --> byte[]
public byte[] intToBytes(int i){
byte[] b = new byte[4];
b[0] = (byte)((i & 0xff000000) >> 24);
b[1] = (byte)((i & 0x00ff0000) >> 16);
b[2] = (byte)((i & 0x0000ff00) >> 8);
b[3] = (byte)(i & 0x000000ff);
return b;
}
반응형