Android Question Convert Byte to Hex code, please help me!

tzfpg

Active Member
Licensed User
Longtime User
I'm using ByteConverter to convert byte to hex, but get the error when debug.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim socket1 As Socket
    Dim AStreams As AsyncStreams
    Dim conv As ByteConverter
End Sub

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    socket1.Initialize("socket1")
    socket1.Connect("192.168.0.153",4001,20000)
End Sub

B4X:
Sub socket1_connected(Successful As Boolean)
        If Successful = False Then
        Msgbox(LastException.Message, "Error connecting")
        socket1.Close
          Activity.Finish
        Return
    Else
        AStreams.Initialize(socket1.InputStream, socket1.OutputStream, "AStreams")
        ToastMessageShow("Connected", True)
    End If
 
End Sub

B4X:
Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    For i = 0 To Buffer.Length - 1
    msg=conv.HexFromBytes(Buffer(i))
    ToastMessageShow(msg,False)
    Next

End Sub

I want to show a hex code one by one.

below is error msg:

Parsing code. 0.02
Compiling code. Error
Error compiling program.
Error description: Cannot cast type: {Type=Byte,Rank=0, RemoteObject=True} to: {Type=Byte,Rank=1, RemoteObject=True}
Occurred on line: 53
msg=conv.HexFromBytes(Buffer(i))
Word: )
 
Last edited:

ernschd

Active Member
Licensed User
Longtime User
First, please use the CODE-Tag for formatting your Source code for better reading.

HexFromBytes reads only a byte array, not a single byte. So you should create a byte array with 1 element
B4X:
   Dim b(1) As Byte   
   For i = 0 To Buffer.Length - 1     
     b(0) = Buffer(i)
     msg = conv.HexFromBytes(b)
     Log(msg)
   Next

Hint: using a Toast is suboptimal because maybe the New_Data Event is raised very often.
 
Upvote 0

tzfpg

Active Member
Licensed User
Longtime User
how can i collect the 24 hex code then show the result? now i'm facing every time the new data arrived less than 24 byte.
but my complete byte is 24 byte. any where can set the buffer to 24 byte, when buffer byte equal to 24 byte then pass to
AStreams_NewData?
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
I don't know if I understand your last question, but you can try to use stringbuilder and substring instructions to "build" your string with the 24 chars ?
 
Upvote 0

tzfpg

Active Member
Licensed User
Longtime User
Sever side every second will send out 24 byte hex code, when the phone connect to server, phone start receive the hex code, but every time receive not enough 24 byte then pass to AStreams_NewData, so the phone only show the not complete hex code.

that is my problem:
server send out - 24 byte -> phone received 15 byte(1st time) show in phone screen
phone received 9 byte(2nd time) show in phone screen
server send out - 24 byte -> phone received 10 byte(3rd time) show in phone screen
phone received 14 byte(4th time) show in phone screen

i want to like this:
server send out - 24 byte -> phone received 24 byte(1st time) show in phone screen
server send out - 24 byte -> phone received 24 byte(2nd time) show in phone screen
server send out - 24 byte -> phone received 24 byte(3rd time) show in phone screen
...........................
 
Upvote 0

tzfpg

Active Member
Licensed User
Longtime User
Prefix mode can't solve my problem. I found the solution, share the code at here hope can help who facing same problem with me:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim socket1 As Socket
    Dim AStreams As AsyncStreams
    Dim conv As ByteConverter
    Dim hex_count As Long
    Dim full_hex As String
End Sub

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim sf As StringFunctions
    sf.Initialize
End Sub

B4X:
Sub AStreams_NewData (Buffer() As Byte)
      Dim msg As String
        If hex_count = 0 Then
            full_hex=""
            Dim b(1) As Byte
            b(0)=Buffer(0)
            msg=conv.HexFromBytes(b)
                If msg="98" Then
                    full_hex=conv.HexFromBytes(Buffer)
                    Dim len_hex As Long = sf.Len(full_hex)
                    hex_count=len_hex
                End If
        Else
            full_hex=full_hex & conv.HexFromBytes(Buffer)
            Dim len_hex As Long = sf.Len(full_hex)
            If len_hex>48 Then
            Dim final_hex As String =sf.Mid(full_hex,1,48)
            Log(final_hex)
            hex_count=0
            End If
        End If
End Sub
 
Last edited:
Upvote 0
Top