Android Question Split 32 bit number into (2) 16 bit numbers

Josh Dobson

Member
Licensed User
Hello all,

I think I need a nudge in the right direction. I need to return (2) 16 bit unsigned ints from a 32 bit unsigned int.
Am I on the right track with this?
B4X:
Sub Upper16bits(str32Bits As String)
    Dim lng32Bits As Long
    lng32Bits = str32Bits                            '0 - 4294967296 (Unsigned 32 bit)
    Dim intUpper16Bits As Int
    intUpper16Bits = Bit.ShiftRight(lng32Bits,16)    '0 - 65536 (Unsigned 16 bit)
    Dim intLower16Bits As Int
    intLower16Bits = Bit.And(lng32Bits,0xFFFF)        '0 - 65536 (Unsigned 16 bit)
End Sub
Thanks guys,
C
 

Josh Dobson

Member
Licensed User
Hello Erel,

Thanks for the guidance. I apologize for not being more clear as to what I'm after. This thread is kinda related to my last one, but slightly different. I figured I would start a new thread for clarity.
I recall reading in another Java forum that the "divide and conquer" routine is the fastest and lightest weight to format a string like I need.
Please feel free to offer any input as to what I'm trying to achieve.
The receiving device is expecting a 10 digit string, (2) 16 bit unsigned integers, zero-padded if required.
Hopefully the following code provides some clarity as to my goal.
Thanks for your help,
C
B4X:
Sub Separate16bits(str32Bits As String)                                    'str32Bits is 0 - 4294967296 from an EditText
    Dim lng32Bits As Long = str32Bits                                    '0 - 4294967296 (Unsigned 32 bit)
    Dim intUpper16Bits As Int = Bit.UnsignedShiftRight(lng32Bits,16)    '0 - 65536 (Unsigned 16 bit)
    Dim intLower16Bits As Int = Bit.And(lng32Bits,0xFFFF)                '0 - 65536 (Unsigned 16 bit)
    FormatOutboundMsg(intUpper16Bits,intLower16Bits)
End Sub

Sub FormatOutboundMsg(UpperInt As Int,LowerInt As Int)
    If UpperInt < 10 Then                                '0 - 9
        OutboundMsg = "0000" & UpperInt                    '0000U
    End If
    If (UpperInt > 9) And (UpperInt < 100) Then            '10 - 99
        OutboundMsg = "000" & UpperInt                    '000UU
    End If
    If (UpperInt > 99) And (UpperInt < 1000) Then        '100 - 999
        OutboundMsg = "00" & UpperInt                    '00UUU
    End If
    If (UpperInt > 999) And (UpperInt < 10000) Then        '1000 - 9999
        OutboundMsg = "0" & UpperInt                    '0UUUU
    End If
    If UpperInt > 9999 Then                                '10000 - 65536
        OutboundMsg = UpperInt                            'UUUUU
    End If

    If LowerInt < 10 Then                                '0 - 9
        OutboundMsg = OutboundMsg & "0000" & LowerInt    'UUUUU0000L
    End If
    If (LowerInt > 9) And (LowerInt < 100) Then            '10 - 99
        OutboundMsg = OutboundMsg & "000" & LowerInt    'UUUUU000LL
    End If
    If (LowerInt > 99) And (LowerInt < 1000) Then        '100 - 999
        OutboundMsg = OutboundMsg & "00" & LowerInt        'UUUUU00LLL
    End If
    If (LowerInt > 999) And (LowerInt < 10000) Then        '1000 - 9999
        OutboundMsg = OutboundMsg & "0" & LowerInt        'UUUUU0LLLL
    End If
    If LowerInt > 9999 Then                                '10000 - 65536
        OutboundMsg = OutboundMsg & LowerInt            'UUUUULLLLL
    End If
End Sub
 
Upvote 0

Josh Dobson

Member
Licensed User
Doh! Don't I feel dumb!
B4X:
OutboundMsg = NumberFormat2(UpperInt,5,0,0,False) & NumberFormat2(LowerInt,5,0,0,False)
Works great.
Thanks all,
C
 
Upvote 0
Top