B4R Question String to Number (again)

daveinhull

Active Member
Licensed User
Longtime User
Hi All,

I know this hs been raised before,but I just can't seem to get it sorted.
I'm trying to conver a string ("240913174545") into a byte array as (24,09,13,17,45,45), i.e. an array of 8 byte with the number representation of each pair of string characters, so "24" string is just 24 (numeric), "09" is 9, "13 is 13, and so on.

I'm justgetting myself completely wrapped up in bytes, strings, array of bytes, array of strings etc.

Any help form anyone would save my sanity

Appreciated
Dave
 

Daestrum

Expert
Licensed User
Longtime User
This will show you how to convert a decimal numeric string into BCD - you may need to change for B4R
B4X:
    Dim st As String = "1234567" ' your string of decimal digits
    If st.Length Mod 2 <> 0 Then st = "0" & st ' correct string if odd length
    Dim d(st.Length/2) As Byte  ' the output byte array
    For a = 0 To st.Length -1 Step 2 ' work on every 2nd char
        Dim b,c As Byte
        'convert to single hex digit and shift left 4 bits
        b = Asc(st.CharAt(a))-0x30
        b = Bit.ShiftLeft(b,4)
        ' convert to single hex digit
        c = Asc(st.CharAt(a+1))-0x30
        ' or b with c to get 2 digit hex and put into array at a/2
        d(a/2) = Bit.Or(c,b)
    Next
    For x = 0 To d.Length -1
        Log(d(x))
    Next
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Hi Daestrum,

Wow, many thanks for the quick reply and sample code. I was only expecting a bit of guidance on the direction to take, but this is brilliant.

I'll work from that. Many thanks
Dave
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Hi all,

For anyone interested, I've done a few mods to Daestrum's code and it now works (as I need it).

B4X:
 Sub DateStringToByteArray (st As String) as byte array
    If st.Length Mod 2 <> 0 Then ' correct string if odd length
        st = "0" + st
    End If
    '
    Dim d(st.Length/2) As byte  ' the output byte array
    '
    ' Loop through the given string 2 bytes at a time
    For a = 0 To st.Length -1 Step 2 ' work on every 2nd char
        '
        Dim b, c As byte
        '
        ' Convert charater 1 of the 2 character value to a number, reference back to vlaue 0 (ASC(48))
        b = BC.StringToBytes(st)(a) - 48
        '
        ' Multiple by 10
        b= b * 10
        '
        ' Convert character 2 of the 2 character value to a number, reference back to value 0 (ASC(48)
        c = BC.StringToBytes(st)(a+1) - 48
        '
        ' Now add the two values together to get the full 8 bit number in decimal
        d(a/2) = b + c
        '
    Next
    '
    Return d
    '
End Sub

Thanks again to Daestrum for giving me the start on it. I did think I could do it without writing code, i.e. just using ByteConverter, but no.

Dave
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Hi (again,

And in a slightly more compact version:

B4X:
 Sub DateStringToByteArray (st As String) As Byte()
    '
    If st.Length Mod 2 <> 0 Then ' correct string if odd length
        st = "0" + st
    End If
    '
    Dim d(st.Length/2) As Byte  ' the output byte array
    '
    ' Loop throgh the given string 2 bytes at a time
    For a = 0 To st.Length -1 Step 2 ' work on every 2nd char
        '
        d(a/2) = (BC.StringToBytes(st)(a) - 48)*10 + BC.StringToBytes(st)(a+1) - 48
        '
    Next
    '
    Return d
    '
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Something desn't feel right with (BC.StringToBytes(st)(a) - 48)*10 - you're treating a hex value as decimal - gut feeling is it should be *16 not *10.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…