Solved

convert two bytes into an integer

Posted on 2000-04-11
7
1,499 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
7 Comments
 
LVL 3

Expert Comment

by:Gordonp
Comment Utility
get rid of the -1 and your fine

MYInt = (z1 * 256) + z2

Gordon

0
 
LVL 3

Expert Comment

by:Lewy
Comment Utility
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
Comment Utility
Adjusted points from 5 to 10
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
What does 'z1 And &H80' mean?
0
 
LVL 2

Expert Comment

by:Crin
Comment Utility
'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
Comment Utility
&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
Comment Utility
Yes it is right
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now