Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sign extension problems...

Status
Not open for further replies.

Digsy

Programmer
Sep 5, 2002
202
GB
hi guys,

I'm trying to form 4 byte values into a long value (i.e. (hex values) bytes FC 24 23 67 becomes long value FC242367) and i keep getting overflow errors which i believe are due to sign extension problems.

Here is a bit of demo code i have been using to form XX000000 (where XX is the byte value)
----
Dim MyByte As Byte
Dim MyLong As Long

MyByte = CByte("&H7F") ' Give byte value of '7F'
MyLong = MyByte * CLng(&H1000000) ' Want MyByte to be MS byte in long
MsgBox Hex(MyLong) ' Gives output of '7F000000'
MyByte = CByte("&H80") ' Now give byte value of '80'
MyLong = MyByte * CLng(&H1000000) ' Want MyByte to be MS byte in long - get overflow here
MsgBox Hex(MyLong)
----

I have actually solved the problem by converting the byte into a string and concatanating the 6 trailing 0's on but i'm sure there must be a better way.

Any ideas?

Dave
 
If you have the four bytes converted to 4 strings, just concatenate the strings:
strTotal = strA & strB & strC & strD

then
msgbox(val("&H" & strTotal))

should give you your long as a hex string

You'll need to error check, of course
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Sorry, should read:

msgbox hex(val("&H" & strTotal))

(I'd edited the 'hex' out to prove it gave a number)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Hi johnwm,

I have actually already done that but i felt that the solution wasnt particularily 'elegant' and that there must be a better way to do it....maybe there isn't.

Dave

 
The problem you have, if you do it with numbers rather than strings, is that VB assumes signed Ints and Longs, so if the MSB becomes 1 then the answer goes negative (1's complement)

That's why I do it the (alright, inelegant) string method.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top