B4J Question [RESOLVED] ParseInt Returning Negative Number for Hex value 'EB'

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I have a hex string - 0C60EB5C87824A355D50C2FCC9F30768CFBB45AC9BCFA1 - I want to decode each hex value back it int value.

B4X:
public Sub hex_decode(pData As String) As String


   Dim x As Int, res As String, h As String, d As Int, b(1) As Byte, c As String
  
   Log("hex_decode :pData len = " & pData.Length)
   Try
       For x = 0 To (pData.Length-1) Step 2
           ' // get hex byet
           h = su.Mid(pData, x, 2)          
           ' // get int value
           b(0) = Bit.ParseInt(h,16)          
           c = bc.StringFromBytes(b,"WINDOWS-1251")
           Log(h & " " & b(0) & " " & Asc(c))
           res = res & c          
       Next
   Catch
       res = ""
   End Try
  
   Log(res)
   Log("hex_decode :res len = " & res.Length)
   Return res


End Sub

Original vb6 Code :

B4X:
Function hex_decode(pData As String) As String

    Dim x As Integer, res As String
   
    On Error GoTo jmp99
   
    For x = 1 To Len(pData) Step 2
        res = res & Chr(CInt("&H" & Mid(pData, x, 2)))
    Next x
      
jmp99:

    If Err.Number <> 0 Then
        hex_decode = pData
    Else
        hex_decode = res
    End If
  

End Function



upload_2018-8-1_13-30-46.png


The above show the original output from VB6 and the output from B4J. 3rd byte in different in B4J

Is the a better way to do this so I get the same result as the vb6 function ?

Any help would be appreciated

Regards

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Your code is a bit strange. Looks like you are using the wrong encoding.

Try this:
B4X:
public Sub hex_decode(pData As String) As String
   Dim bc As ByteConverter
   Dim b() As Byte = bc.HexToBytes(pData)
   Return BytesToString(b, 0, b.Length, "windows-1252")
End Sub


thanks Erel, encoding issue
 
Upvote 0
Top