0

I currently have an application where I receive 2 values e.g. 0e 15 through bluetooth. I now want to display these in decimal values.

The code is as follows:


    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


    public static String formatHexString(byte[] data) {
        return formatHexString(data, false);
    }

    public static String formatHexString(byte[] data, boolean addSpace) {
        if (data == null || data.length < 1)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
            if (addSpace)
                sb.append(" ");
            
        }
        return sb.toString().trim();
    }
    
}

How would I go about into converting this string so I can decimal values back?

Sifu Duck
  • 23
  • 7
  • 1
    Java >= 17 has `java.util.HexFormat` – g00se Jan 16 '23 at 09:32
  • 1
    [Hex2Int](https://stackoverflow.com/a/5886656/3636601), [int2hex](https://www.tutorialspoint.com/java-program-to-convert-integer-to-hexadecimal) – Jens Jan 16 '23 at 09:36
  • 1
    from title "*How to convert Hex to decimal*" -> so you want to have the `byte[] data` represented as decimals? Simplest solution: `Arrays.toString(data);` - e.g. if `data = new byte[] { 0x0e, 0x15}`, that method will return `"[14, 21]"` **OR** from question "*converting this string so I can decimal values*" -> which string? (eventually `new BigInteger("0e15", 16).toByteArray()` - check negative values, e.g. first byte>= 0x80) – user16320675 Jan 16 '23 at 10:12
  • 1
    If your app receives two bytes then it just receives two bytes. And those bytes are not decimal or hexadecimal. They are just eight bits each. It is to you if you display the value of these bytes in decimal or hexadecimal notation. So your question is wrong as there is nothing to convert from hex. – blackapps Jan 16 '23 at 10:42
  • @blackapps exactly, I was confused as I thought I had to use the same method in both environments but I could've just simply read them out in decimal value in the first place. – Sifu Duck Jan 16 '23 at 11:16

2 Answers2

1

You can use java.lang.Integer library's method parseInt.

simply do Integer.parseInt(hexString,16);

For more information you can refer to documentation here.

Satya
  • 11
  • 1
0

I was confused as I somehow thought bytes had to be displayed in hexadecimal value. It didn't cross my mind that I could just showcase two bytes in decimal value. Currently have what I want with the following code:

public class HexUtil {
    public static String formatDecimalString(byte[] data, boolean addSpace) {
        if (data == null || data.length < 1)
            return null;
        int negative = data[0] < 0 ? -1 : 1;
        BigInteger bigInteger = new BigInteger(negative, data);
        String decimal = bigInteger.toString();
        if(addSpace) {
            decimal = decimal.replaceAll("(.{2})", "$1 ");
        }
        return decimal;
    }
}
Sifu Duck
  • 23
  • 7