J Josh Dobson Member Licensed User Sep 23, 2017 #1 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? Code: Copy 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
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? Code: Copy 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
Erel B4X founder Staff member Licensed User Longtime User Sep 24, 2017 #2 Why is the input a string? I believe that you should use UnsignedShiftRight instead of ShiftRight. Note that you can assign the value when you dim the variable: Code: Copy Dim lng32Bits As Long = str32Bits Upvote 0
Why is the input a string? I believe that you should use UnsignedShiftRight instead of ShiftRight. Note that you can assign the value when you dim the variable: Code: Copy Dim lng32Bits As Long = str32Bits
J Josh Dobson Member Licensed User Sep 24, 2017 #3 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 Code: Copy 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
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 Code: Copy 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
J Josh Dobson Member Licensed User Sep 24, 2017 #4 Doh! Don't I feel dumb! Code: Copy OutboundMsg = NumberFormat2(UpperInt,5,0,0,False) & NumberFormat2(LowerInt,5,0,0,False) Works great. Thanks all, C Upvote 0
Doh! Don't I feel dumb! Code: Copy OutboundMsg = NumberFormat2(UpperInt,5,0,0,False) & NumberFormat2(LowerInt,5,0,0,False) Works great. Thanks all, C