Android Question About Bit.ParseInt function

vincentehsu

Member
Licensed User
Longtime User
Could anybody answer me what's going on?
B4X:
Dim t(10) As Byte
t(0)=0x00
    t(1)=0x2e
    t(2)=0x6a
    t(3)=0x9c
    t(4)=0x01
    t(5)=0x00
    t(6)=0x9f
    t(7)=0x02
    t(8)=0x06
    t(9)=0x00
    Dim i As Int
    For i=0 To t.Length-1
        Log(Bit.ParseInt(t(i),10))
    Next

log file
B4X:
0
46
106
-100
1
0
-97
2
6
0
 

vincentehsu

Member
Licensed User
Longtime User
by the way ,how can i do the xor function like
B4X:
t(1) xor (t2) xor (t3) xor (t4) xor (t5) xor (t6) xor (t7)
 
Upvote 0

picenainformatica

Active Member
Licensed User
Longtime User
B4X:
Dim j AsInt=t(1)
Dim i as int
For i=2 To 7
   j=j xor Bit.ParseInt(t(i),16))
next
 
Upvote 0

vincentehsu

Member
Licensed User
Longtime User
B4X:
Dim t(10) As Byte
t(0)=0x00
    t(1)=0x2e
    t(2)=0x6a
    t(3)=0x9c
    t(4)=0x01
    t(5)=0x00
    t(6)=0x9f
    t(7)=0x02
    t(8)=0x06
    t(9)=0x00
    Dim i As Int
    For i=0 To t.Length-1
        Log(Bit.ParseInt(t(i),10))
    Next

My understanding : my original byte is base 16 and i wanna convert to base 10 , i should use Bit.ParseInt(t(i),10) right?
on my log file
B4X:
0
46
106
-100
1
0
-97
2
6
0

there are only 2 bytes convert error at t(3) and t(6) why?
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
My understanding : my original byte is base 16 and i wanna convert to base 10 , i should use Bit.ParseInt(t(i),10) right?
on my log file
no, the bytes are stored as base 2. Your source code is using base 16, but the values are converted to base 2 during compiling.

there are only 2 bytes convert error at t(3) and t(6) why?
There is no error, it is just a misunderstanding of how Bytes work in Java and misuse of ParseInt().

Bytes are stored as signed values in the range of -128 to 127. If you try and assign a value higher than 127, it will be interpreted as a two's compliment value and stored as a negative value. The binary representation will be the same, but will be treated as a negative when used in math or printed to the screen. SO when you are storing 0x9c in a byte, it is stored as -100 instead.

ParseInt() is not for converting bytes to Ints, instead it is for converting strings to ints. Passing bytes, then converting to Int really does nothing except eat up processing cycles.

To correctly convert all the Bytes into Ints, you just need to cast to the correct type and mask the result with Bit.AND().
B4X:
    Dim t(10) As Byte
    t(0)=0x00
    t(1)=0x2e
    t(2)=0x6a
    t(3)=0x9c
    t(4)=0x01
    t(5)=0x00
    t(6)=0x9f
    t(7)=0x02
    t(8)=0x06
    t(9)=0x00
    Dim i As Int
    For i=0 To t.Length-1
        Log(Bit.AND(t(i),0xFF))
    Next
The Bit.AND takes an Int as its first parameter. We are passing a Byte, but it is internally cast to an Int. The 0xFF will strip off the sign bits, leaving the original value instead of its negative version.

Results:
B4X:
0
46
106
156
1
0
159
2
6
0
 
Upvote 0

vincentehsu

Member
Licensed User
Longtime User
I see, what if i wanna convert to real binary like "0010",what i should do?
and I wanna do the xor function like
B4X:
t(1) xor (t2) xor (t3) xor (t4) xor (t5) xor (t6) xor (t7)
in the earlier post picenainformatica gave me the code
B4X:
For i=2 To 7
   j=j xor Bit.ParseInt(t(i),16))
Next
it's present syntax
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
I'm not sure what picenainformatica is trying to accomplish. xor isn't even a B4A keyword. To xor two numbers together, you need to use Bit.Xor() function. Also, you don't need to convert to binary, everything is already stored as binary in memory. If you mean how to convert to a string representing the binary value, you use Bit.ToBinaryString()
B4X:
    Dim i As Byte
    Dim j As Byte = 0
    Dim t(10) As Byte
    t(0)=0x00
    t(1)=0x2e
    t(2)=0x6a
    t(3)=0x9c
    t(4)=0x01
    t(5)=0x00
    t(6)=0x9f
    t(7)=0x02
    t(8)=0x06
    t(9)=0x00
    For i = 0 To 9
        j = Bit.Xor(t(i),j)
    Next
    Log (Bit.ToBinaryString(j))
 
Upvote 0
Top