int与byte、byte[]相互转换

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wenqiangluyao/article/details/100161769

一、int to byte

范围在 -128 ~ 127之间可以直接转换,超出这个范围就乱了。

int i = 127;
byte b = (byte) i;
Integer i = 127;
i.byteValue();

二、byte to int

// byte to int (int仍有正负)
static int byte2Int(byte byt){
    // 直接强转
    return (int) byt;

}
// byte to int (int无符号)
static int byte2UnsignedInt(byte byt){
    /* 与强转的区别:强转之后表示数值不变(不论正数、0、负数),如-120强转后还是-120,
    ************* 但是这种方式强转之后,正数和0也不变,但是负数会变成:输入值+2<sup>8</sup>,如 -120 转换后变为136,即-120+256
    */
    return Byte.toUnsignedInt(byt);
    //return ((int) byt) & 0xFF; // Byte.toUnsignedInt(byt)就是这样实现的
}

三、int to byte[]

方式一

低位在前高位在后
/**  
    * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用 
    * @param value  
    *            要转换的int值 
    * @return byte数组 
    */    
public static byte[] intToBytes( int value )   
{   
    byte[] src = new byte[4];  
    src[3] =  (byte) ((value>>24) & 0xFF);  
    src[2] =  (byte) ((value>>16) & 0xFF);  
    src[1] =  (byte) ((value>>8) & 0xFF);    
    src[0] =  (byte) (value & 0xFF);                  
    return src;   
}  
高位在前低位在后
 /**  
    * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。  和bytesToInt2()配套使用 
    */    
public static byte[] intToBytes2(int value)   
{   
    byte[] src = new byte[4];  
    src[0] = (byte) ((value>>24) & 0xFF);  
    src[1] = (byte) ((value>>16)& 0xFF);  
    src[2] = (byte) ((value>>8)&0xFF);    
    src[3] = (byte) (value & 0xFF);       
    return src;  
}  

方式二

低位在前高位在后
/**  
    * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。  
    * @param value  
    *            要转换的int值 
    * @return byte数组 
    */    
public static byte[] intToBytes(int value)   
{   
    byte[] byte_src = new byte[4];  
    byte_src[3] = (byte) ((value & 0xFF000000)>>24);  
    byte_src[2] = (byte) ((value & 0x00FF0000)>>16);  
    byte_src[1] = (byte) ((value & 0x0000FF00)>>8);    
    byte_src[0] = (byte) ((value & 0x000000FF));          
    return byte_src;  
}  

三、byte[] to int

方式一

低位在前高位在后
/**  
    * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用 
    *   
    * @param src  
    *            byte数组  
    * @param offset  
    *            从数组的第offset位开始  
    * @return int数值  
    */    
public static int bytesToInt(byte[] src, int offset) {  
    int value;    
    value = (int) ((src[offset] & 0xFF)   
            | ((src[offset+1] & 0xFF)<<8)   
            | ((src[offset+2] & 0xFF)<<16)   
            | ((src[offset+3] & 0xFF)<<24));  
    return value;  
}  
高位在前低位在后
/**  
    * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用 
    */  
public static int bytesToInt2(byte[] src, int offset) {  
    int value;    
    value = (int) ( ((src[offset] & 0xFF)<<24)  
            |((src[offset+1] & 0xFF)<<16)  
            |((src[offset+2] & 0xFF)<<8)  
            |(src[offset+3] & 0xFF));  
    return value;  
}  

方式二

低位在前高位在后
 /**  
    * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序。 
    *   
    * @param ary  
    *            byte数组  
    * @param offset  
    *            从数组的第offset位开始  
    * @return int数值  
    */    
public static int bytesToInt(byte[] ary, int offset) {  
    int value;    
    value = (int) ((ary[offset]&0xFF)   
            | ((ary[offset+1]<<8) & 0xFF00)  
            | ((ary[offset+2]<<16)& 0xFF0000)   
            | ((ary[offset+3]<<24) & 0xFF000000));  
    return value;  
}  

参考:
byte与int强制转换
【转】java中byte, int的转换, byte String转换
byte[]数组和int之间的转换

猜你喜欢

转载自blog.csdn.net/wenqiangluyao/article/details/100161769