Lheonair
Lheonair

Reputation: 498

Unsigned bytearray to HEX

I'm new to working with bytes and I have to filter out some BLE messages by some values on their Payload (bytearray)

According to the documentation of the device I'm working with, the payload 3th and 4th value corresponds to the Company ID. The values I get are [-38, 3] and the documentation states that this value should be 0x03DA. Are these values compatible at all? How should I translate that to be sure it's OK?

Upvotes: 0

Views: 126

Answers (1)

ukBaz
ukBaz

Reputation: 7954

You don't say what language you are doing this in but there are some general points that can be made about Bluetooth data.

From https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers/ :

If the value in hex is 0x03DA, decimal 986 and it is for EnOcean GmbH. enter image description here

Bluetooth transmits it bytes in little endian format so the bytes would be [0xDA, 0x03] or [218, 3]. The reason you are seeing the -38 is because the byte is being treated as a signed integer (sometimes referred to as a short) not an unsigned integer (unsigned short).

In Python this would look like:

>>> int.from_bytes(b'\xDA\x03', byteorder='little', signed=False)
986
>>> hex(int.from_bytes(b'\xDA\x03', byteorder='little', signed=False))
'0x3da'

Upvotes: 1

Related Questions