Android Question 0xFO in arrary : -16 (0xFFFFFFF0)

Lello1964

Well-Known Member
Licensed User
Longtime User
I need to build an array of bytes containing hexadecimal values: (0x32, 0xF0, 0xE0). When I insert the values into the array, I get:
50 (0x32)
-16 (0xFFFFFFF0)
-32 (0xFFFFFFE0)

Instead, I need them to remain in the original format: (0x32, 0xF0, 0xE0).

I tried using

B4X:
 Messaggio() As Byte = ARRAY AS BYTE(0x32, Bit.And(0xF0, 0xFF), Bit.And(0xE0,0xFF))

but it doesn't work.

Do you have a solution?
 

drgottjr

Expert
Licensed User
Longtime User
@emexes recently reported he was somewhere with spotty internet (we wish him godspeed). if he's at home, it's lunchtime. in the meantime, you should know that in java, bytes are signed and with a maximum value of 127, so although your bit op is correct, the result is an int array, not a byte array.
B4X:
dim Messaggio() As int = ARRAY AS int(0x32, Bit.And(0xF0, 0xFF), Bit.And(0xE0,0xFF))
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I need to build an array of bytes containing hexadecimal values: (0x32, 0xF0, 0xE0). When I insert the values into the array, I get:
50 (0x32)
-16 (0xFFFFFFF0)
-32 (0xFFFFFFE0)

Instead, I need them to remain in the original format: (0x32, 0xF0, 0xE0).

I tried using

B4X:
 Messaggio() As Byte = ARRAY AS BYTE(0x32, Bit.And(0xF0, 0xFF), Bit.And(0xE0,0xFF))

but it doesn't work.

Do you have a solution?
It's no problem, they are stored in array of byte as (0x32, 0xF0, 0xE0). they just are displayed as signed int, if you would like to show them as unsigned byte, you can do bit and 0xff before show them.
B4X:
    Dim Messaggio() As Byte = Array As Byte(0x32, 0xf0, 0xe0)
    Dim bc As ByteConverter
    Log(bc.HexFromBytes(Messaggio)) '32F0E0' see they are correct
    For Each b As Byte In Messaggio
        Log(Bit.And(b, 0xff)) '50 240 224'
    Next
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
@emexes will show you how to do with bit operations.

Lol I just got home, saw this post, and you would be correct but for that @teddybear has already posted precisely what I would have posted.

Except maybe that I'd have used For I = 0 to Messaggio.Length - 1 rather than For Each in case I needed to refer to more than one element of the array at the same time.
 
Upvote 0

emexes

Expert
Licensed User
I need to build an array of bytes containing hexadecimal values

Zooming out a little to see the larger picture... what is the array going to be used for for?

Three is an unusual length, and the three values you mention 0x32 0xF0 0xE0 don't look quite right together either (not saying they're wrong, just saying that they're not a protocol/format/structure/layout that I know offhand).
 
Upvote 0
Top