B4J Question convert problem hex to integer (B4J<>B4A)

Tayfur

Well-Known Member
Licensed User
Longtime User
Hello
I send data to with UPD from Ardunio to PC like under lines

sending data is HEX :E517C1029A02AE08

I double cheked with ardiuno logs with HAX data .
My hex data is inculde 4 uint values
E517 ->1. uint
C102->2. uint
9A02->3. uint
AE08->4. uint

B4X:
' *******THIS IS ARDIUNO CODE****************
rr=bc.HexFromBytes(Main.Read8(rn))
Log("sonuc:",rr) '>>> sonuc:E517C1029A02AE08
Dim bx(2),mr(8) As Byte
mr=Main.Read8(rn)
bc.ArrayCopy2(mr,0,bx,0,2)
Log(bc.UIntsFromBytes(bx)(0)) '>> 6117   (uint values)
bc.ArrayCopy2(mr,2,bx,0,2)
Log(bc.UIntsFromBytes(bx)(0))' >> 705  (uint values)
bc.ArrayCopy2(mr,4,bx,0,2)
Log(bc.UIntsFromBytes(bx)(0)) '>> 666 (uint values)
bc.ArrayCopy2(mr,6,bx,0,2)
Log(bc.UIntsFromBytes(bx)(0))'>>2222 (uint values)

After I recive with my PC (Java)
and I convert hex to int.
But valus not same with Ardunio uint values.

How can solve it?

B4X:
' *******THIS IS JAVA CODE on PC****************
Sub Cihaz_data_koundu_R10(no As Int,msg As String) 'ip port geldi
    Log(msg)         '>>> E517C1029A02AE08
    Log(msg.Length)        '>>>16
    Dim sayi As Int
    sayi=Bit.ParseInt(msg.SubString2(0,4),16)
    Log(sayi)         '>>>58647    (it is not same Arduino values =6117)
    sayi=Bit.ParseInt(msg.SubString2(4,8),16)
    Log(sayi)            '>>>49410 (it is not sameArduino values =705)
    sayi=Bit.ParseInt(msg.SubString2(8,12),16)
    Log(sayi)            '>>>39426 (it is not sameArduino values =666)
    sayi=Bit.ParseInt(msg.SubString2(12,16),16)
    Log(sayi)            '>>>44552 (it is not sameArduino values =2222)

End Sub
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Check they are working to the same endian. As the values you gave match an endian mismatch.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
On a pc (for example) 0xe517 Is calculated as 0xe5*256 + 0x17 = 58647
if the endian is wrong it will calculate it as 0x17*256 + 0xe5 = 6117

The byte order is important.

I would guess the string you get back reads
17E502C1029A08AE
instead if
E517C1029A02AE08 as you expected
 
Last edited:
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
On a pc (for example) 0xe517 Is calculated as 0xe5*256 + 0x17 = 58647
if the endian is wrong it will calculate it as 0x17*256 + 0xe5 = 6117

The byte order is important.

I would guess the string you get back reads
17E502C1029A08AE
instead if
E517C1029A02AE08 as you expected
Yessss; it is perfect description.
Thank you for helps. I will try it.
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
On a pc (for example) 0xe517 Is calculated as 0xe5*256 + 0x17 = 58647
if the endian is wrong it will calculate it as 0x17*256 + 0xe5 = 6117

The byte order is important.

I would guess the string you get back reads
17E502C1029A08AE
instead if
E517C1029A02AE08 as you expected


I cant find correct values.
Do you have any idea?

B4X:
' ı used  byteconverter lib.
Dim b1(1),b2(1) As Byte
    b1=x.HexToBytes("E5")
    b2=x.HexToBytes("17")
    Dim i As Int
    i=b1(0)*256+b2(0)  '>> b1= -27  /// b2=23
    Log(i) '>>> -6889
    i=b2(0)*256+b1(0)
    Log(i)'>>> 5861
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
To test you could try changing
B4X:
 sayi=Bit.ParseInt(msg.SubString2(0,4),16)
to
B4X:
 sayi=Bit.ParseInt(msg.SubString2(2,4)&msg.SubString2(0,2),16)
 
Upvote 0

ta1dr

Member
Licensed User
Longtime User
kolay gelsin bu sayı dönüşümlerinde 3-4 gün ugraşırım bende ...:)

Good luck with...
the value conversions are a difficult issue for me :)
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
kolay gelsin bu sayı dönüşümlerinde 3-4 gün ugraşırım bende ...:)

Good luck with...
the value conversions are a difficult issue for me :)

ilk kez donanım programlama yapınca oluyor sıkıntı.

ben çözdüm o sırada ama amele yöntem olduydu.;)
B4X:
Dim b(8) As Byte
    Dim x As ByteConverter
    Dim z As Int
    b=x.HexToBytes(msg)
Dim i(8) As Int
   
    For f=0 To 7
        If b(f)<0 Then i(f)=b(f)+256 Else i(f)=b(f)   
    Next
sayi =i(1)*256+i(0)


Correct way...
B4X:
sayi=Bit.ParseInt(msg.SubString2(2,4)&msg.SubString2(0,2),16)
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
1. Note that B4J is not Java. Please edit the title and change it to B4J.

2. There are several errors in your code. You don't need to convert the bytes to a hex string. Just send the bytes directly to B4J.
B4R UInt is a 2 bytes number. The same as B4J Short.

Thanks @Erel, its works.
Now it is very clean code ;)

B4X:
Dim x As ByteConverter
Dim s(4) As Short
    x.LittleEndian=True
    s=x.ShortsFromBytes(x.HexToBytes(msg))
's(0) >> 6117
's(1)>>705
's(2)>>666
's(3)>>2222
 
Upvote 0
Top