Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fun with HEX.and &H

Status
Not open for further replies.

GFrantsen

Programmer
Sep 16, 2002
37
0
0
US
I am having a problem with the HEX() and &H functions.
I need to turn &HFFFF into 65532, not -1. Can anyone tell me how to do this?

If you do a HEX(65532) you get FFFF.
If you do a HEX(-1) you get FFFF.

I'm assuming because the default &H has something to do with being signed. How do I UNsign it?

Constants will NOT help. I need an unsigned coversion for ANY hex number.

Thanks in advance.
Greg.
 
All the '&H' and '&HFFF&' doesn't actually affect the underlying numbers.

&HFFFF is only a different way of viewing 65535. It doesn't alter it's value.

Just dim your variables as doubles, do straightforward arithmetic as required and send the answer back. VB will worry about the final conversion to Long!

Private Sub Command1_Click()
Dim mylong As Double
Dim myleft As Double
Dim myright As Double
myright = val(Text1)
myleft = val(Text2)

MsgBox "L = " & myleft & " R = " & myright & " Number = " & (65536 * myleft + myright)
End Sub

(I haven't put any error checking in as I don't expect this to be part of any real code) Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
No, it won't. Greg's problem is that he is getting anything up to &HFFFFFFFF back in a Long from a Sub or Function (eg, waveOutGetVolume). You can assign this to a double if you like, but VB will cast it to an signed integer. Eg:
[tt]
Option Explicit
Private Type HexHack
HiLong As Long
LoLong As Long
End Type

Private Sub Command1_Click()
Dim hLong As Long
Dim dDouble As Double
Dim myHack As HexHack

dDouble = HexSource(hLong)
' OK, dDouble and hLong are now equal to &HFFFFFFFF,
' which means VB sees BOTH of them as -1
' Don't just try and print it out. Have a look in the Locals window.


' OK, now hack it
HackHex hLong, myHack

'Display results
Debug.Print hLong
Debug.Print dDouble
Debug.Print myHack.HiLong
Debug.Print myHack.LoLong
'Beep

End Sub

Private Function HexSource(ByRef Hexy As Long) As Long
Hexy = &HFFFFFFFF
HexSource = Hexy
End Function

Private Sub HackHex(ByVal lHex As Long, tHexHack As HexHack)
tHexHack.HiLong = ((lHex And &HFFFF0000) / &H10000) And &HFFFF&
tHexHack.LoLong = lHex And &HFFFF&
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top