iOS Question Weird substring2 bug...

techknight

Well-Known Member
Licensed User
Longtime User
I am running into a weird bug with iOS. It may be in android as well, I havent tested.

I am sitting here testing my packet receive/parsing routine for robustness, and it failed of course. With this weird bug:

Let a string called RxPacket be this:

RxPacket = "01A00304"

This is a string representing hex data, but its still a string...

So, Given the VB function of this:

B4X:
TestLength = HexString(Mid(RxPacket, 3, 2)) 'Send Length byte  <--- Returns "A0" which

causes a crash of:

CRASH: Line 597 Cannot parse: A0

Line 597 is the Return Text.Substring2 from my Mid subroutine.

Now figure this: If I change A0, to 9F or smaller, It doesnt crash! it works fine. But >9F causes crash.

Keep in mind this is all technically "strings" at this point, Substring2 should function regardless of the data inside.

Subroutine Mid:

B4X:
Sub Mid(Text As String, Start As Int, Length As Int) As String
    Return Text.SubString2(Start-1,Start+Length-1)
End Sub

Sub HexString(Number As Byte) As String
Dim retVal As String
Dim retByte(0+1) As Byte
Dim Convert As ByteConverter

retByte(0) = Number

retVal = Convert.HexFromBytes(retByte)

Return retVal
End Sub

any ideas? I know I am passing a string into a byte with HexString, but I changed it to Val, same error...
 
Last edited:

techknight

Well-Known Member
Licensed User
Longtime User
Another thing that I need to figure out is this:

RxPacket = RxPacket & BytesToString(Buffer, 0, Buffer.Length, "UTF8")

This is my receive routine, But I need to test and make sure the data coming in is ACTUALLY a string... Because, if I send random bytes the program crashes: Error decoding data as string.

So, hmm....
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Nevermind, that was a simple fix. Had to make sure there was no byte in the buffer greater than 7F or 127, or the UTF8 thing barks and bursts into flames.
 
Upvote 0
Top