Bug? bc.Substring2

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

While playing with the Strings and Bytes tutorial I tried to extract items from a string using Substring2 with strange results. Not sure if this a bug or not but Substring works as expected but not Substring2.


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
End Sub

Private Sub AppStart
  Serial1.Initialize(115200)
  Log("AppStart") 
  Dim bc As ByteConverter
  Dim b() As Byte = bc.Trim("abcdef ")
  b(0) = Asc("M") 'this line will change the value of the literal string
  Log("b length = ",b.Length)
  Dim s As String = "abcdef "
  Log(s) 'Mbcdef
  Log("abcdef ") '???
 
  For i = 0 To b.Length - 1
      Log("b.",i," = ",b(i))
  Next
  Dim d As String
   d = bc.StringFromBytes(b)
   Log("d length = ", d.Length)
  For i = 0 To d.Length -1
      ' this next line messes the Arduino up completly
     ' Log("d = ",bc.SubString2(d,i,1))
    ' this line works as expected
    Log("d = ",bc.SubString(d,i))
  Next
End Sub

Using Substring the log shows
Appstart
b length = 6
Mbcdef
abcdef
b.0 = 77
b.1 = 98
b.2 = 99
b.3 = 100
b.4 = 101
b.5 = 102
d length = 6
d = Mbcdef
d =bcdef
d = cdef
d = cd
d = c

Using Substring2 the log shows
g������;�a��/��o����.���?���/��|�޿U߽:_���/o����U�z����3���.ӕ�k��������ws���?�ڻ�����[������������~�zַx~^�;���v͞]��{��V���}�����+>�˻�t��?��}�=Sٟv��������~���,{^���.�w�����ӹ��>(*�����ɹ�����5�yb���j�uO����k���.{���r�s�����{������[��E���g�����_����:�>�����3������?���{���뻽�������7��{��c?ܜ���_�o�~�n|�owս��Y}v�˟o���y�ۭ�����u�w����VcU��O����<������s�]�

Also when I copy stuff from the logs window using "Copy all to clipboard", everything I copy is pasted backwards.
d = fd = efd = defd = cdefd = bcdefd = Mbcdefd length = 6b.5 = 102b.4 = 101b.3 = 100b.2 = 99b.1 = 98b.0 = 77abcdef Mbcdef b length = 6AppStart

This is the info from the logs window but backwards. ie the first log line should be "Appstart"

Thanks

PS. Really love B4R. Thanks for another great product
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The third parameter is the EndIndex, not the length (this is the same as in other B4X tools). The EndIndex is exclusive.
B4X:
bc.SubString2(d,i, i + 1) 'correct code

Note that it is almost never needed to convert an array of bytes to a string.
You can access the bytes directly:
B4X:
For i = 0 To b.Length - 1
 Log(b(i))
Next

The logs are copied correctly here. Does it happen all the time?
 

atiaust

Active Member
Licensed User
Longtime User
The third parameter is the EndIndex, not the length (this is the same as in other B4X tools). The EndIndex is exclusive.
B4X:
bc.SubString2(d,i, i + 1) 'correct code

Note that it is almost never needed to convert an array of bytes to a string.
You can access the bytes directly:
B4X:
For i = 0 To b.Length - 1
Log(b(i))
Next

The logs are copied correctly here. Does it happen all the time?

I just copied this from the log using "Copy all to clipboard" and pasted it below.

102101100999877d = fd = ed = dd = cd = bd = Md length = 6b.5 = 102b.4 = 101b.3 = 100b.2 = 99b.1 = 98b.0 = 77abcdef Mbcdef b length = 6AppStart

Capture.jpg

This is what it looks like in the logs.
 

atiaust

Active Member
Licensed User
Longtime User
Does it work properly in other B4X tools?

Just Copied from a B4J app.

Guess = 7Guess = 6Guess = 5Guess = 4Guess = 3Guess = 2Guess = 1Program started.

By way of explanation.
I am running B4R and B4J on a Windows 10 laptop (this machine).
If I used "Copy all to clipboard" and pasted to a Word doc all is OK but if I copy using B4R or B4J and paste into this reply its backwards.

I tried with B4J and B4A on my Windows 7 machine and it is OK so it appears its a Windows 10 issue.

Hope this is clear...
 

Similar Threads

Top