Hex to string

PHB2

Member
Licensed User
Longtime User
I am using Web services (thru HttpUtils) and need to process the response from a string of hex to the ASCII string equivalent.

My code is here:
B4X:
Sub JobDone (Job As String)
    If HttpUtils.IsSuccess(PostUrl) Then
   
      Dim data As String
      data = HttpUtils.GetString( PostUrl )
      
      
      Dim bytearray(0) As Byte
      bytearray = bconv.HexToBytes( data )
      
      Dim msg As String
      msg = bconv.StringFromBytes( bytearray, "UTF8" )
      
      Log( msg )
      
      Msgbox( msg, "" )
    Else
        ToastMessageShow("Error sending request - are you sure you are connected to the internet?", True)
    End If
    HttpUtils.Complete = False 'Turn off the complete flag so we won't handle it again if the activity is resumed.
End Sub

(bconv is an instance of ByteConverter library)
(I know the encoding is UTF8 as I am also doing a very similar piece of code in a c# app and decode it as UTF8)

The response I get back is:

7b2273756363657373223a747275652c226572724d7367223a6e756c6c2c2247554944223a2234656465313536612d333336642d346536322d386330612d643231643964353031623636222c227573657244657461696c73223a22222c22757365724e616d65223a2231222c2270617373776f7264223a2261346179632f38302f4f47646134424f2f316f2f56306574704f71694c78314a77423553336265485730733d222c2266756c6c4e616d65223a224d61726b2048222c2274656d70223a5b224974656d2031222c224974656d2032222c224974656d2033225d2c226372656174656444617465223a225c2f4461746528313332353732313630303030302b30303030295c2f222c226c6173744c6f67676564496e44617465223a6e756c6c7d

The text I would expect it to convert to is:

Any suggestions?
 

PHB2

Member
Licensed User
Longtime User
Hi Steve

That was the post I used to craft my code in the 1st place. Since it looks so easy to do, I am wondering what I am doing wrong.

Thanks for the suggestion though.
 
Upvote 0

mleffler

Member
Licensed User
Longtime User
Possible solution

Take a look at BytesToString. I use it in some programs for the same thing you are trying to do.
 
Upvote 0

PHB2

Member
Licensed User
Longtime User
Fixed it - it was me being an idiot.

The reponse text that comes back from the web service call had quotes at the beginning and end of the string, which was then throwing out the conversion from hex to bytes. Replacing the quotes with "", then doing the hex to byte conversion fixed it.
 
Upvote 0
Top