Hex <----> Decimal conversions 
Author Message
 Hex <----> Decimal conversions

Hey everyone, thanks for your previous help..  One thing I need to do in my
current project is convert a Hex number to a Decimal.  You can easily
convert decimal to Hex by using the Hex() function.. but how do you convert
a Hex string back to Decimal?  I would think it would be just as easy, but
I've been searching the help file for the past hour, and I havent found
anything.   Any help is appreciated ! :)

 - QuantuZ



Tue, 27 Nov 2001 03:00:00 GMT  
 Hex <----> Decimal conversions
Just prefix the actual hex digits by &H. For example

Dim myvar as long
myvar = &HFF

VB will happily accept this - and it will result in myvar having the value
255. There are, however, little traps for the unwary. If, for example, you
used:

Dim mvar as long
myvar = &HFFFF

then you would expect myvar (a 4 byte long number) to have the value 65535,
but it doesn't. It actually gets the value -1. VB "sees" the 4 hex digits
FFFF and assumes that this is a 2 byte integer (since it is a valid number
in such an integer). Now &HFFFF is -1 (as an integer), and so VB passes this
value to myvar (even though myvar is a 4 byte long number). Using myvar =
CLng (&HFFFF) makes no difference. The trick is to always use the correct
number of hex digits to suit the type of variable (byte, integer or long)
which you are working with. The VB editor, of course, will not allow you to
type &H0000FFFF. Oh no! it is to *clever* for that. It thinks that we are
dummies and have typed lots of leading zeros by mistake - so it removes them
(try it)! The best way, which avoids all confusion, is to always type and
work with all hex numbers as strings, and always use the correct number of
hex digits for the type of variable (byte, single or long), including any
leading zeros:

Dim myvar as long
myvar = "&H0000FFFF"

Mike


Quote:
> Hey everyone, thanks for your previous help..  One thing I need to do in
my
> current project is convert a Hex number to a Decimal.  You can easily
> convert decimal to Hex by using the Hex() function.. but how do you
convert
> a Hex string back to Decimal?  I would think it would be just as easy, but
> I've been searching the help file for the past hour, and I havent found
> anything.   Any help is appreciated ! :)

>  - QuantuZ



Tue, 27 Nov 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. <<<<<<<<ComboBox>>>>>>>>>>>>

2. decimal <-> fraction conversion

3. HELP >>>>>>WIN API <<<<<<<<<<

4. help me out please!!!!<<<<<<<<<<<<<<<<<<<<<<<thanks>>>>>>>>>>>>>>>>>>>>>>>>

5. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

6. <<<<HELP- OLE container Control>>>>>>>>>

7. VB5<->RDO2<->ODBC<->ORACLE Procedure Problem

8. <<<Cneck Box>>>

9. <<<Gif Files>>>

10. <<<CD ROM>>>

11. <<<Win32Api.TXT>>>

12. <<<NetWork Drv>>>

 

 
Powered by phpBB® Forum Software