B4J Question How to convert and put String ="67" to Buffer() as byte

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear frinds, please for help,

i have four string
B4X:
dim number1 as string = "250"
dim number2 as string = "12"
dim number3 as string = "1"
dim number4 as string = "32"
dim sendbyte() as byte
            sendbyte(0)=number1
            sendbyte(1)=number2
            sendbyte(2)=number3
            sendbyte(3)=number4
receive(sendbyte)
and I need to send this strings to this roputine

B4X:
Sub receive(buffer() As Byte)
    Dim s As String
    s = BytesToString(buffer, 0, buffer.Length, "UTF8")

    Dim find32 As String =s.CharAt(0)
    Dim data32() As Byte
    data32=find32.GetBytes("UTF8")
    Dim value As Byte=0
    value=data32(0)
    Log("value=" & value)
    If value32=32 Then log("Was received 32")
End sub

How can I send strings to this routine please? I am trying
Ints to bytes and string to bytes, but I have not results..
Best regards
p4ppc
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
s = BytesToString(buffer, 0, buffer.Length, "UTF8")
the bytes you added are NOT a STRING!

why not accessing the array directly?

B4X:
Sub receive(buffer() As Byte)
    Dim value As Byte=buffer(0)
    Log("value=" & value)
    If value=32 Then log("Was received 32")
End sub
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
DonManfred,

thank you for your answer..Yes this is for me trouble, I can not solve this..
...difficult for me is creating Bytes() form four strings

I can not create four bytes from four strings. This bytes I can not send to
B4X:
Sub receive(buffer() As Byte)

Please for help..

EDIT
I get in string "67" and I must convert it to byte1=67, then byte2, byte3, byte4 and together I need send it to Receive sub
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Don Manfred,

please string "250" I need send as first (one) byte
string "5" I need send as second (one) bytep
string "2" I need send as third (one) byte
string "32" I need send as four (one) byte

and this four bytes I need send to Receive sub

Thank you
p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Excuse me for my style of describing this...

I get four strings, four numbers:
B4X:
dim number1 as string = "250"
dim number2 as string = "67"
dim number3 as string = "1"
dim number4 as string = "32"

and I want send them as four bytes to receive Sub
please
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Daestrum, DonManfred,

yes, this is solution, this is what I am trying to solve.THANK YOU VERY MUCH
Best regards
p4ppc
 
Upvote 0

emexes

Expert
Licensed User
and I need to send this strings to this roputine
B4X:
Sub receive(buffer() As Byte)
    Dim s As String
    s = BytesToString(buffer, 0, buffer.Length, "UTF8")

    Dim find32 As String =s.CharAt(0)
    Dim data32() As Byte
    data32=find32.GetBytes("UTF8")
    Dim value As Byte=0
    value=data32(0)
    Log("value=" & value)
    If value32=32 Then log("Was received 32")
End sub
That is one mixed-up roputine ;-)

I've got ten minutes to spend, so let's go through it section-by-section when it handles the incoming packet of four bytes:
hex: 0xFA 0x0C 0x01 0x20
dec: 250 12 1 32
B4X:
'so far, so good: buffer(0) = 250, buffer(1) = 12, buffer(2) = 1, buffer(3) = 32
Sub receive(buffer() As Byte)

    'convert four bytes of UTF-8 to Unicode String
    'this is where we run off the rails
    '250 is never ever ever a valid UTF-8 byte
    'but presumably you are expecting it to return a four-character string,
    'so let's run with that
    Dim s As String
    s = BytesToString(buffer, 0, buffer.Length, "UTF8")

    'this will get the first character of s - Chr(250)? -
    'and put it into a one-character string find32
    Dim find32 As String =s.CharAt(0)

    'convert that one-character string into a one-byte array data32
    'now we have the opposite UTF-8 problem in that U+00FA is > ASCII, and thus will
    'not fit into one UTF-8 byte, instead it will become two bytes: 0xC3 0xB8
    Dim data32() As Byte
    data32=find32.GetBytes("UTF8")

    'take the first byte of data32() which is hex: 0xC3, or dec: 195
    'and put it into value
    Dim value As Byte=0
    value=data32(0)

    'tbh all bets are off at this point, there are so many possibilities
    'of how that invalid UTF-8 got handled
    Log("value=" & value)

    'I doubt you'll be seeing this log
    If value32=32 Then log("Was received 32")

End sub
'also, wondering why first letter of "log" and "sub" not capitalized by the IDE???
'has this code actually been run???
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Instead, maybe try this:
B4X:
Sub Receive(Buffer() As Byte)

    Dim LS As String = "Buffer() length " & Buffer.Length & " ="
    For I = 0 To Buffer.Length - 1
        LS = LS & " " & Buffer(I)
    Next I
    Log(LS)

    If Buffer.Length = 4 Then
        If Buffer(0) = 250 Then    'sync byte (I am guessing; could be wrong)
            If Buffer(1) = 12 Then    'packet id byte (I am guessing; could be wrong)
                If Buffer(2) = 1 Then    'number of data bytes to follow (I am guessing; could be wrong)
                    Dim Value As Byte = Buffer(3)
                    Call HandleNewValue(Value)
                End If
            End If
        End If
    End If

End Sub

Sub HandleNewValue(Value As Byte)

    Log("value=" & Value)

    'already logged above, but apparently we are logging Value 32 twice
    If Value = 32 Then
        Log("Was received 32")
    End If

End Sub
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear EMEXES,DAESTRUM,DONMANFRED,

thank you very much for you advices and examples, thank you for time spending on this solution,
it is SOLVED,
BEST REGARDS
p4ppc
 
Upvote 0

emexes

Expert
Licensed User
it is SOLVED
Ripper :)

Watch out for those UTF8 conversions. ASCII characters 0..127 convert to bytes 1:1, and any characters beyond that convert to multiple bytes (ie: 2, 3 or 4 bytes).

If your serial communication protocol (packet format) is fixed-length bits and bytes, then it is usually better to use byte arrays rather than strings.
 
Upvote 0

emexes

Expert
Licensed User
Also, whenever you have a series of validity checks like this:
B4X:
If Buffer.Length = 4 Then
    If Buffer(0) = 250 Then    'sync byte (I am guessing; could be wrong)
        If Buffer(1) = 12 Then    'packet id byte (I am guessing; could be wrong)
            If Buffer(2) = 1 Then    'number of data bytes to follow (I am guessing; could be wrong)
                Dim Value As Byte = Buffer(3)
                Call HandleNewValue(Value)
            End If
        End If
    End If
End If
you can write it in this style instead (is sometimes better, also easier indentation):
B4X:
If Buffer.Length <> 4 Then
    Log("Invalid length")
Else If Buffer(0) <> 250 Then
    Log("Invalid sync byte")
Else If Buffer(1) <> 12 Then
    Log("Invalid packet id byte")
Else If Buffer(2) <> 1 Then
    Log("Invalid data length")
Else    'nothing bad above, therefore must be good... ;-)
    Dim Value As Byte = Buffer(3)
    Call HandleNewValue(Value)
End If
 
Upvote 0
Top