int和byte之间的转换

参考自:http://blog.csdn.net/nupt123456789/article/details/8047619


int32byte8

例如:int类型:97

则二进制表示为:00000000 00000000 00000000 01100001

Java语言

/*将int转为低字节在前,高字节在后的byte数组
b[0] = 11111111(0xff) & 01100001
b[1] = 11111111(0xff) & (n >> 8)00000000
b[2] = 11111111(0xff) & (n >> 8)00000000
b[3] = 11111111(0xff) & (n >> 8)00000000
*/
public byte[] IntToByteArray(int n) {  
        byte[] b = new byte[4];  
        b[0] = (byte) (n & 0xff);  
        b[1] = (byte) (n >> 8 & 0xff);  
        b[2] = (byte) (n >> 16 & 0xff);  
        b[3] = (byte) (n >> 24 & 0xff);  
        return b;  
}
//将低字节在前转为int,高字节在后的byte数组(与IntToByteArray1想对应)
public int ByteArrayToInt(byte[] bArr) {  
         if(bArr.length!=4){  
             return -1;  
         }  
         return (int) ((((bArr[3] & 0xff) << 24)    
                    | ((bArr[2] & 0xff) << 16)    
                    | ((bArr[1] & 0xff) << 8)
| ((bArr[0] & 0xff) << 0)));   
}

Java语言

/*将int转为低字节在后,高字节在前的byte数组
b[0] = 11111111(0xff) & 01100001
b[1] = 11111111(0xff) & 00000000
b[2] = 11111111(0xff) & 00000000
b[3] = 11111111(0xff) & 00000000
*/
public byte[] IntToByteArray2(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数组(与IntToByteArray2想对应)
public int ByteArrayToInt2(byte[] bArr) {  
         if(bArr.length!=4){  
             return -1;  
         }  
         return (int) ((((bArr[0] & 0xff) << 24)    
                    | ((bArr[1] & 0xff) << 16)    
                    | ((bArr[2] & 0xff) << 8)
| ((bArr[3] & 0xff) << 0)));   
}


byte之间互转string

/** 
     * 将byte数组转化成String,为了支持中文,转化时用GBK编码方式 
     */  
    public String ByteArraytoString(byte[] valArr,int maxLen) {  
        String result=null;  
        int index = 0;  
        while(index < valArr.length && index < maxLen) {  
            if(valArr[index] == 0) {  
                break;  
            }  
            index++;  
        }  
        byte[] temp = new byte[index];  
        System.arraycopy(valArr, 0, temp, 0, index);  
        try {  
            result= new String(temp,"GBK");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
    /** 
     * 将String转化为byte,为了支持中文,转化时用GBK编码方式 
     */  
    public byte[] StringToByteArray(String str){  
        byte[] temp = null;  
        try {  
            temp = str.getBytes("GBK");  
        } catch (UnsupportedEncodingException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return temp;  
    } 



C++

void  IntToByteArray(int i,byte *bytes,int size = 4)  
  
{  
     //byte[] bytes = new byte[4];  
    memset(bytes,0,sizeof(byte) *  size);  
    bytes[0] = (byte) (0xff & i);  
    bytes[1] = (byte) ((0xff00 & i) >> 8);  
    bytes[2] = (byte) ((0xff0000 & i) >> 16);  
    bytes[3] = (byte) ((0xff000000 & i) >> 24);  
    return ;  
 }  
  
//byte转int  
 int ByteArrayToInt(byte* bytes,int size = 4)   
{  
    int addr = bytes[0] & 0xFF;  
    addr |= ((bytes[1] << 8) & 0xFF00);  
    addr |= ((bytes[2] << 16) & 0xFF0000);  
    addr |= ((bytes[3] << 24) & 0xFF000000);  
    return addr;  
 }



猜你喜欢

转载自blog.csdn.net/qq_28468727/article/details/52498510
今日推荐