Solved

# High Byte

Posted on 1997-12-03
516 Views
I need to split and integer in to bytes.

I have suceded getting the low byte doing a bitwise AND with 255.

How can i get the high byte of an integer??
0
Question by:PedroMVGomes
• 4

LVL 8

Accepted Solution

mrmick earned 50 total points
ID: 1443381
Function HiByte(Pass As Integer) As Byte

If (Pass And -2 ^ 15) = 0 Then
HiByte = Int(Pass / 255)
Else
HiByte = ((Pass And 32512) \ 256) Or 128
End If

End Function

0

LVL 8

Expert Comment

ID: 1443382
Simplified a bit...

Function HiByte(i As Integer) As Byte

If i And -2 ^ 15 Then
HiByte = ((i And 32512) \ 256) Or 128
Else
HiByte = i \ 255
End If

End Function

0

LVL 8

Expert Comment

ID: 1443383

Function HiByte(i As Integer) As Byte
Dim Sign As Boolean

Sign = i And -2 ^ 15
HiByte = ((i And 32512) \ 256) Or (128 And Sign)

End Function

Note: All three work!

0

LVL 6

Expert Comment

ID: 1443384
yes, yet another way, and without stripping the sign bit first

In case you'd like to see it in decimal:

Function HiByte(i As Integer) As Byte

HiByte = (i And 65280) / 256 And 255

End Function

0

LVL 8

Expert Comment

ID: 1443385
How can i do the reverse, this is i have to bytes the low one and the higher one how can a calculate the integer??
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

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

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