Android Question Math with bytes

Johan Schoeman

Expert
Licensed User
Longtime User
I am testing the following code:

B4X:
    Dim bb As Byte = -104
    Log("bb = " & bb)
    
    bb = bb + 256
    Log("bb + 256 = " & bb)

I need "bb" to be 152 after "bb + 256" was executed. But I am getting "-104". How do I get this to be 152?

Can someone please help with this? Even trying Bit.And(bb, 0xFF) gives -104 and not 152.
 

Star-Dust

Expert
Licensed User
Longtime User
Byte (-127 .. 127)
se this

B4X:
Dim bb As Byte = -104
Log("bb = " & bb)
  
Dim bs As Short = 256 + bb
Log("bb + 256 = " & bs)
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Byte (-127 .. 127)
se this

B4X:
Dim bb As Byte = -104
Log("bb = " & bb)
 
Dim bs As Short = 256 + bb
Log("bb + 256 = " & bs)
Where have you been all day.....! ;)

B4X:
    Dim bb As Byte = -104
    Log("bb = " & bb)
    
    Dim bs As Short = 256 + bb
    bb = bs
    Log("bbnew + 256 = " & bs)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I read you carefully, but I did not understand the problem.:confused:
Unfortunately I do not understand English well.:p

On the other hand I understand B4X and with the example I understood the problem ;)
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I read you carefully, but I did not understand the problem.:confused:
Unfortunately I do not understand English well.:p

On the other hand I understand B4X and with the example I understood the problem ;)
OK - same problem. How do I get bb to be 152. Logging bs gives 152 but doing bb = bs and then logging bb still gives -104. I need bb (byte) to be 152....

The below code logs -104 for bb but 152 for bs. I need bb to be 152...

B4X:
    Dim bb As Byte = -104  
    Dim bs As Short = 256 + bb
    bb = bs
    Log("bbnew + 256 = " & bb)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
The byte type can not contain a positive number superior to 127.
They are 8 bits, it first bit is used only for the sign. The other 7 bits are used to represent a number 0 127
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
The byte type can not contain a positive number superior to 127.
They are 8 bits, it first bit is used only for the sign. The other 7 bits are used to represent a number 0 127
Hmmmm.....that is true. Need to rethink how I am going to solve this. Issue is that there are a number of bytes in the byte() array that are less than 0 and they all yield "0xfffd" and not the hex values that I need.

Thanks for your help. Much appreciated!
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Instead of bytes, you can use char and then do a casting with int?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Issue is that there are a number of bytes in the byte() array that are less than 0 and they all yield "0xfffd" and not the hex values that I need.
You are doing something. Post the relevant code. In most cases it shouldn't matter that the bytes are signed bytes.

You can convert a signed byte to unsigned value with:
B4X:
Dim ub As Int = Bit.And(0xff, SignedByte)
 
Upvote 0
Top