Solved

convert two bytes into an integer

Posted on 2000-04-11
7
1,500 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 36
VBA filters 2 50
Help with retrieving partial value from a column using VB.NET 4 39
fso.FolderExists("\\server\HiddenFolder$") 4 46
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

932 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

13 Experts available now in Live!

Get 1:1 Help Now