B4J Question string conversion

G.Z.

Member
Hi All,

I need help in string conversion.

I wrote this code:

Dim B3S As String
B3S = "W"
Select B3S
Case 87
Log ("is 87")​
Case "W"
Log ("is W")​
End Select​

In the Select I woudl like to recognise B3S not like X but as 87 (that is the ASCII value of X), how I can convert B3S to jump in Case 87?

Thank you
 

G.Z.

Member
yes is working, but in case Asc(VAR1) if the value of VAR1 is bigger then 128 it give 65533.
Other suggestion pls?
 
Upvote 0

G.Z.

Member
Hi,
this is the code.

I'm receiving 5 bytes from UART (B1S,B2S,B3S,B4S,B5S) and I have to take action on their value. The valid value of byte could be from 0 to 255

B4X:
Sub AStream_NewData (Buffer() As Byte)
[INDENT]Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
s = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
HalfBuffer = HalfBuffer & s
Do While HalfBuffer.Length >= 5
[INDENT]NrLoop = NrLoop + 1
HalfBufferTmp = HalfBuffer.SubString2(0, 5)
B1S = HalfBufferTmp.SubString2(0, 1)
B2S = HalfBufferTmp.SubString2(1, 2)
B3S = HalfBufferTmp.SubString2(2, 3)
B4S = HalfBufferTmp.SubString2(3, 4)
B5S = HalfBufferTmp.SubString2(4, 5)
Log(Asc(B1S) & "." & Asc(B2S) & "." & Asc(B3S) & "." & Asc(B4S) & "." & Asc(B5S))   ' ---->>> LOG is: 65533.1.3.20.2 (byte from UART was 203.1.3.20.2)
Log(B1S & "." & B2S & "." & B3S & "." & B4S & "." & B5S)                                        ' ---->>> LOG is: �....  (byte from UART was 203.1.3.20.2)
B3S = Asc(B3S)
Select B3S
[INDENT]Case 3
[INDENT]Log ("is 3")
If Asc(B1S) = 202 Then    ' <<<<<----- trouble is here, if the value of B1S in more then 127 the value from Asc become 65533
[INDENT]If Asc(B2S) = 1 Then
[INDENT]TAint = Asc(B4S)
TAdec = Asc(B5S)
Log ("TA : " & B4S & "." & B5S)[/INDENT]
End If[/INDENT]
End If[/INDENT]
Case Else
[INDENT]Log (B3S)[/INDENT][/INDENT]
End Select
astream.Write(HalfBuffer.GetBytes("ASCII")) 'ECHO
HalfBuffer = HalfBuffer.SubString (5)[/INDENT]
Loop
NrLoop = 0[/INDENT]
End Sub

In SELECT or IF I need to use the value from 0 to 255 to recognise the value of the data, any suggestion?
 
Last edited:
Upvote 0

G.Z.

Member
How to get bytes from AStream_NewData and move them directly to 5 byte var (ex: B1 B2 B3 B4 B5)?
In this case I can use SELECT ....CASE 202 ?
 
Upvote 0

G.Z.

Member
I tried this but seem not working:

B4X:
Sub AStream_NewData (Buffer() As Byte)
     Dim BB(5) As Byte = Buffer
     Log ("BB="&Asc(BB(0)))
     If BB(0) = 202 Then ' <<<--- check if value is 202, byte value is 202 but it IF don't recognise it
          Log ("BB=202")
     Else
          Log ("BB= not 202")
     End If
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Bytes in Android are signed (-128 to 127).

You have two options to compare the values:
1. Only use bytes:
B4X:
Dim b As Byte = 202
If Buffer(0) = b Then
 ...
Else

2. Convert the byte to unsigned byte (which will be stored in an int):
B4X:
If ToUnsigned(Buffer(0)) = 202 Then
 ...
Else

...

Sub ToUnsigned (b As Byte) As Int
 Return Bit.And(0xFF, b)
End Sub

Note that you don't need the BB variable. Work with Buffer instead.
 
Upvote 0
Top