B4R Question Failed to convert string to int correctly...

hatzisn

Expert
Licensed User
Longtime User
Hi everybody,

I am completely lost with this... I download a string from my site which looks like this "0~20200105~152425". I try to convert the first character to an int. How can someone explain this nonsense? See the following code in JobDone Sub and the logs.

B4X:
    If Job.Success Then
        Dim bc As ByteConverter
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters

'        Log(bc.SubString2(Job.Response,0,1))
'        Log(bc.SubString2(Job.Response,2,6))
'        Log(bc.SubString2(Job.Response,6,8))
'        Log(bc.SubString2(Job.Response,8,10))
'        Log(bc.SubString2(Job.Response,11,13))
'        Log(bc.SubString2(Job.Response,13,15))
'        Log(bc.SubString2(Job.Response,15,17))


        Dim s As String
        s = bc.SubString2(Job.Response,0,1)
        Log(s)
        iDoW = Bit.ParseInt(s, 10)
        Log(iDoW)
        If iDoW = 0 Then iDoW = 7
        Log(iDoW)

    End if



DEBUG StatusLogger Appender Console stopped with status true
TRACE StatusLogger XmlConfiguration stopped 2 remaining Appenders.
TRACE StatusLogger XmlConfiguration cleaning Appenders from 2 LoggerConfigs.
DEBUG StatusLogger Stopped XmlConfiguration[location=jar:file:/C:/B4X/Tools/B4R/arduino-1.8.10/lib/pde.jar!/log4j2.xml] OK
DEBUG StatusLogger Stopped LoggerContext[name=af3868, org.apache.logging.log4j.core.LoggerContext@10ab13f] with status true
********************* PROGRAM STARTING ****************
@��`���-z��RS��{̍ʕ�X��B)�E#���ر$�X�̟f蘜ᄦD�ᄦD��ZME��mAppStart
Connected to router.
*******************************
JobName: Example
Response: 0~20200105~152425
1073686188
9900
9900

I have an intuition that it has to do with the fact that the site sends data communication in UTF8 but I do not know how to face it... I use a WeMos D1 V3

Thanks in advance
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User
It is not a UTF8 matter. The Job.Response.Length equals to 17 which is the correct length of the string.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Let me check the Warnings...
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
No Warnings. Check the attached file.

The value 17 is the Log(Job.Response.Length) to check if it is a unicode matter.
 

Attachments

  • LogsNoWarnings.txt
    135.9 KB · Views: 268
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'll help you.

1. bc.SubString will return an array of bytes.
2. Arrays of bytes cannot be implicitly cast to strings. You should see a warning about it.
3. In this case you do need a string as only strings can be parsed to numbers.
4. Use bc.StringFromBytes to get a string:
B4X:
Dim s As String = bc.StringFromBytes(bc.SubString2(Job.Response, 0, 1))
'or directly to a number:
Dim i As Int = bc.StringFromBytes(bc.SubString2(Job.Response, 0, 1))

You should see a compiler warning. It will not appear in the logs.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Once again you are right. Works perfect... Thanks.
 
Upvote 0
Top