convert two bytes into an integer

Is this right?

eg
dim z1 as byte
dim z2 as byte
dim MyInt as integer

MyInt = -1 * (z1 * 256 + z2)

LVL 1
MitchBroadheadAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
GordonpConnect With a Mentor Commented:
&H80 is a hex number = 128

(z1 And &H80) returns 128(&H80) is z1 is >= 128 and 0 if z1 < 128

Could rewrite it

IF z1 >= 128 then
   MyInt = ((z1 - 128) * 256 + z2) * -1
else
   MyInt = z1 * 256 + z2
End If

which is the same as lewys answers but using maths rather than bitwise manipulations.

Gordon
0
 
GordonpCommented:
get rid of the -1 and your fine

MYInt = (z1 * 256) + z2

Gordon

0
 
LewyCommented:
You must test and handle the case where z1 is greater than 127 (The MyInt is negative). If not you gen an Overflow error

    If z1 And &H80 Then
        MyInt = ((z1 And &H7F) * 256 + z2) Or &H8000
    Else
        MyInt = z1 * 256 + z2
    End If
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MitchBroadheadAuthor Commented:
Adjusted points from 5 to 10
0
 
MitchBroadheadAuthor Commented:
What does 'z1 And &H80' mean?
0
 
CrinCommented:
'z1 And &h80' maens logical binary operator. Select "And" word within VB environment and press F1 for more info.

Sincerely yours,

Crin
0
 
itacanCommented:
Yes it is right
0
All Courses

From novice to tech pro — start learning today.