Solved

convert two bytes into an integer

Posted on 2000-04-11
7
1,505 Views
Last Modified: 2012-06-22
Is this right?

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

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

0
Comment
Question by:MitchBroadhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:Gordonp
ID: 2703980
get rid of the -1 and your fine

MYInt = (z1 * 256) + z2

Gordon

0
 
LVL 3

Expert Comment

by:Lewy
ID: 2704005
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
 
LVL 1

Author Comment

by:MitchBroadhead
ID: 2704056
Adjusted points from 5 to 10
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:MitchBroadhead
ID: 2704057
What does 'z1 And &H80' mean?
0
 
LVL 2

Expert Comment

by:Crin
ID: 2704102
'z1 And &h80' maens logical binary operator. Select "And" word within VB environment and press F1 for more info.

Sincerely yours,

Crin
0
 
LVL 3

Accepted Solution

by:
Gordonp earned 10 total points
ID: 2704114
&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
 

Expert Comment

by:itacan
ID: 2704138
Yes it is right
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question