0

Values from the notification should be hexadecimal but I got decimal and negative values (with another app I got the good format) :

123 0 0 72 98 0  
124 0 0 39 97 0   
125 0 0 -2 95 0 
126 0 0 -50 94 0 
127 0 0 -105 93 0  
-128 0 0 88 92 0  
-127 0 0 18 91 0  
-126 0 0 -59 89 0 
-125 0 0 113 88 0 
-124 0 0 22 87 0 
-123 0 0 -76 85 0  
-122 0 0 76 84 0  
-121 0 0 -35 82 0  
-120 0 0 103 81 0

Do you know how can I get this in Hexadecimal form?

Best regards

2 Answers2

0

The RxAndroidBle library emits a byte[] when a notification is received. A single byte value can be represented in a different ways.

The log you have presented is just printing the raw values according to Java specification. Java does not have a concept of unsigned values and every value that starts with a 1 bit is treated as a negative number. i.e. 0xA2 is equivalent to -94. (0xA2 == -94) == true

A simple way to display a value as a hex is to use String.format() i.e. String.format("0x%2xh", byteValue)

Most probably you wanted to ask a question: How to convert a byte array to a hex String in Java

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
0

I wrote a little method to convert a byte[] into a String that looks like [02 FC 41 5A] etc.:

static String byteArrayToHexDigits(final byte[] bytes) {
        int i;
        StringBuilder result = new StringBuilder( "[" );
        for( i = 0; i < (bytes.length-1); i++ ) {
            result.append(String.format( "%02X  ", bytes[i] ));
        }
        result.append(String.format( "%02X]", bytes[i] ));
        return result.toString();
    }
Robert Lewis
  • 1,847
  • 2
  • 18
  • 43