ringo4269
03-17-2003, 07:00 PM
I'm sending a signed 32 bit number via serial port to my vb program.
What is the best way to convert the 4 bytes that come in into 1 32 bit number?
I'm trying this code:
Dim left_enc As Long
Dim right_enc As Long
right_enc = right_enc + (CLng(Serial_Buffer(6))*CLng(16777216)) '2^24
right_enc = right_enc + (CLng(Serial_Buffer(7)) * CLng(65536)) '2^16
right_enc = right_enc + (CLng(Serial_Buffer(8)) * CLng(256)) '2^8
right_enc = right_enc + CLng(Serial_Buffer(9))
left_enc = left_enc + (CLng(Serial_Buffer(10))*CLng(16777216)) '2^24
left_enc = left_enc + (CLng(Serial_Buffer(11)) * CLng(65536)) '2^16
left_enc = left_enc + (CLng(Serial_Buffer(12)) * CLng(256)) '2^8
left_enc = left_enc + CLng(Serial_Buffer(13))
The problem is that when I send a negative number I get an overflow because a long is +- 2 billion and it thinks is a number something like 4Billion instead of a -5.
Thanks,
Ringo
What is the best way to convert the 4 bytes that come in into 1 32 bit number?
I'm trying this code:
Dim left_enc As Long
Dim right_enc As Long
right_enc = right_enc + (CLng(Serial_Buffer(6))*CLng(16777216)) '2^24
right_enc = right_enc + (CLng(Serial_Buffer(7)) * CLng(65536)) '2^16
right_enc = right_enc + (CLng(Serial_Buffer(8)) * CLng(256)) '2^8
right_enc = right_enc + CLng(Serial_Buffer(9))
left_enc = left_enc + (CLng(Serial_Buffer(10))*CLng(16777216)) '2^24
left_enc = left_enc + (CLng(Serial_Buffer(11)) * CLng(65536)) '2^16
left_enc = left_enc + (CLng(Serial_Buffer(12)) * CLng(256)) '2^8
left_enc = left_enc + CLng(Serial_Buffer(13))
The problem is that when I send a negative number I get an overflow because a long is +- 2 billion and it thinks is a number something like 4Billion instead of a -5.
Thanks,
Ringo