B4R Question Convert String to Number

Helmut S

Member
Licensed User
Longtime User
Hello everybody,

I have a problem when I try to convert a string to a numeric value.

Example:

I get a DateTime value in the following message "DT20170920111250S??" with the emedded date of "20170920" (09/20/2017) and the embedded time of "111250" (11:12:50).

I want to call the following Sub (to set the RTC), but I have to pass the parameters in numeric form (as bytes) and not the ASCII value:

setDS3231time(50, 12, 11, 0, 20, 9, 17)

B4X:
Sub setDS3231time(second As Byte, minute As Byte, hour As Byte, dayofweek As Byte,dayOfMonth As Byte, months As Byte, years As Byte)
 
    'First byte=0 --> Set Next input To start at the seconds register
    'Next bytes (in BCD): second,minutes, hours, dayOfWeek,dayOfMonth,month,year
    wirevar.WriteTo(RTC_ADDRESS,Array As Byte(0,decToBcd(second),decToBcd(minute),decToBcd(hour),decToBcd(dayofweek),decToBcd(dayOfMonth),decToBcd(months),decToBcd(years)))

End Sub

How can I do this correctly? Any help is highly appreciated.

H.
 

Cableguy

Expert
Licensed User
Longtime User
String manipulation is a tricky thing in b4R... as they are immutable...
I think you still could just extract the values directly to int variable without creating "temp" strings...
Something like

B4X:
Dim myDateTime as string = myreceivedvalue
Dim year as int = myDateTime.substring2(3,7)
Repeat for each int variable
 
Upvote 0

Helmut S

Member
Licensed User
Longtime User
Hi Cableguy,

sorry, but that doesn't work, since the String Type has no substring2() and even if you took the substring2() from ByteConverter te result cannot be assigned to an int.
 
Upvote 0

Misterbates

Active Member
Licensed User
Does B4R have Regex? If so then the pattern [^0-9]*(.{4})(.{2})(.{2})(.{2})(.{2})(.{2}) would split the string into the necessary pieces.

Could each of those pieces then be assigned to an Int? If not then a small utility routine to convert ASCII chars to the equivalent numbers (select case "0", case "1" etc.) should do the trick.
 
Upvote 0

Helmut S

Member
Licensed User
Longtime User
Thanks to all of you, I finally found out myself, and it was quite simple, indeed. :rolleyes:

Here's the code that works:

B4X:
                Dim dt() As Byte = bc.StringFromBytes(Message)
                Dim sMyyear As Int = bc.StringFromBytes(Array As Byte(dt(5), dt(6)))
                Dim sMyMonth As Int = bc.StringFromBytes(Array As Byte(dt(7), dt(8)))
                Dim sMyDay As Int = bc.StringFromBytes(Array As Byte(dt(9), dt(10)))
                Dim sMyHour As Int = bc.StringFromBytes(Array As Byte(dt(11), dt(12)))
                Dim sMyMinute As Int = bc.StringFromBytes(Array As Byte(dt(13), dt(14)))
                Dim sMySecond As Int = bc.StringFromBytes(Array As Byte(dt(15), dt(16)))
               
                setDS3231time(sMySecond, sMyMinute,sMyHour,0,sMyDay,sMyMonth,sMyyear)

Again, thanks to you all.

H.
 
Last edited:
Upvote 0

Helmut S

Member
Licensed User
Longtime User
That's true, I tried to do so, but it didn't work. Took me some time to figure out this solution, even if it's possibly not the best one. ;)
 
Upvote 0
Top