Android Question Split integer into bytes

Mitesh_Shah

Member
Licensed User
Longtime User
Hi How to splite intiger value in bytes ?

B4X:
dim on_time as int

dim on_hr as byte

dim on_min as byte


on_time = 1234        '// 12 Hour and 34 minutes want to Sprite hours and minutes into 2 separate byte on_Hr $ on_min


'// Want result >>

on_hr = 12

on_min = 34
 

emexes

Expert
Licensed User
B4X:
on_min = on_time Mod 100
on_hr = (on_time - on_min) / 100    'the subtraction isn't strictly necessary but it avoids the question about rounding when casting Double to Int
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Or as String:
B4X:
If on_time >= 0 And on_time <= 2400 Then    'because some people differentiate between midnight at start of day, and midnight at end of day
    Dim HHMM As String = on_time
    Do While HHMM.Length < 4
        HHMM = "0" & HHMM
    Loop
    on_hr = HHMM.SubString2(0, 2)
    on_min = HHMM.SubString(2)    'same as .SubString(2, 4)
Else
    Log("invalid time")
End If
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Or as String but you don't like the concatenating:
Poor Man's RSET:
Dim XHHMM As String = on_time + 10000    'assuming on_time is 0..2400 then XHHMM will be 10000..12400 ie 5 digits
on_hr = XHHMM.SubString2(1, 3)
on_min = XHHMM.SubString(3, 5)
 
Upvote 0
Top