byte array to decimal convertion in java

前端 未结 4 1055
傲寒
傲寒 2020-12-22 03:59

I am new to java. I receive the UDP data in byte array. Each elements of the byte array have the hexadecimal value. I need to convert each element to integer.

How to

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 04:42

    Function : return unsigned value of byte array.

    public static long bytesToDec(byte[] byteArray) {
        long total = 0;
        for(int i = 0 ; i < byteArray.length ; i++) {
            int temp = byteArray[i];
            if(temp < 0) {
                total += (128 + (byteArray[i] & 0x7f)) * Math.pow(2, (byteArray-1-i)*8); 
            } else {
                total += ((byteArray[i] & 0x7f) * Math.pow(2, (byteArray-1-i)*8));
            }
        }
        return total;
    }
    

提交回复
热议问题