byte array to unsigned int in java

后端 未结 4 1914
暗喜
暗喜 2021-01-05 04:34

I\'m converting a byte array into int by doing this:

ByteArrayInputStream bais = new ByteArrayInputStream (data);
DataInputStream dis = new Data         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 05:22

    Each cell in the array is treated as unsigned int:

    private int unsignedIntFromByteArray(byte[] bytes) {
        int res = 0;
        if (bytes == null)
            return res;
    
        for (int i = 0; i < bytes.length; i++) {
            res = (res *10) + ((bytes[i] & 0xff));
        }
        return res;
    }
    
    • The code will convert [12,34] to 1234

提交回复
热议问题