Android Question Convert Int to bytes

hanyelmehy

Active Member
Licensed User
Longtime User
i use this code to send beacon data
B4X:
Dim bb As B4XBytesBuilder 'B4XCollections
bb.Initialize
bb.Append(Array As Byte(0x02, 0x15))
Dim bc As ByteConverter
bb.Append(bc.HexToBytes("0CF052C297CA407C84F8B62AAC4E9020"))
bb.Append(Array As Byte(0, 9, 0, 6, 0xb5))
Dim b() As Byte = bb.ToArray
peripheral.ManufacturerData = CreateMap(76: b )

this line
bb.Append(Array As Byte(0, 9, 0, 6, 0xb5))
write Major and minor values
first two bytes are first and second byte of Major

my question is how to convert int value to present first and second byte
in other way if i want to send major value 300 what i should write
 

emexes

Expert
Licensed User
this line
bb.Append(Array As Byte(0, 9, 0, 6, 0xb5))
write Major and minor values
first two bytes are first and second byte of Major

my question is how to convert int value to present first and second byte
in other way if i want to send major value 300 what i should write

300 / 256 = 1 remainder 44____'256 = 2^8 = "column value" of byte (similar to 10 being the "column value" of decimal numbers)

thus the two bytes would be 1 and 44

but there is eternal debate about which way around they should be. šŸ™ƒ

ByteConverter can convert your 16-bit number 300 into two 8-bit numbers eg:

B4X:
Dim bc as ByteConverter
bc.LittleEndian = False

'''bb.Append(Array As Byte(0, 9, 0, 6, 0xb5))
'''replaced by / same as :

bb.Append(bc.ShortsToBytes(Array As Short(9)))    'major version
bb.Append(bc.ShortsToBytes(Array As Short(6)))    'minor version
bb.Append(Array As Byte(0xb5))                    'something else
 
Last edited:
Upvote 0
Top