I have a small routine which I pass a 32-Bit Long Variable, and 4 8-Bit Byte Variables, which will break up the value of the long into its 4 Byte Values.
The following routine I wrote worked fine for long value which are positive.
The problem I am encountering now is that when you try and pass the Long value as a negative number the sub, an overflow occurs, because Bytes only go from 0-255. Is there anyway to handle negative numbers properly? Any changes or modifications to the above routine?
Any and all help would be greatly appreciated.
Best regards,
Andrew Peskin
[email protected]
------------------
Code:
Long = WWWWWWWW XXXXXXXX YYYYYYYY ZZZZZZZZ Byte 1 = ZZZZZZZZ Byte 2 = YYYYYYYY Byte 3 = XXXXXXXX Byte 4 = WWWWWWWW
Code:
Sub Break_Long_To_Bytes(L1 As Long, B1 As Byte, B2 As Byte, B3 As Byte, B4 As Byte) Dim LNG_1 As Long '// Holding variable for B1 Dim LNG_2 As Long '// Holding variable for B2 Dim LNG_3 As Long '// Holding variable for B3 Dim LNG_4 As Long '// Holding variable for B4 Dim TMPlng As Long '// Temporary Long variable used for shifting TMPlng = L1 LNG_1 = TMPlng Mod 256 TMPlng = TMPlng \ 256 '// Shift 1 Byte LNG_2 = TMPlng Mod 256 TMPlng = TMPlng \ 256 '// Shift 1 Byte again LNG_3 = TMPlng Mod 256 TMPlng = TMPlng \ 256 '// Shift 1 Byte again LNG_4 = TMPlng Mod 256 B1 = LNG_1 '// Set the Most Right Byte -- ZZZZZZZZ B2 = LNG_2 '// Set the Second Right Byte -- YYYYYYYY B3 = LNG_3 '// Set the Second Left Byte -- XXXXXXXX B4 = LNG_4 '// Set the Most Left Byte -- WWWWWWWW End Sub
Any and all help would be greatly appreciated.
Best regards,
Andrew Peskin
[email protected]
------------------
Comment