Click to See Complete Forum and Search --> : Convert hexadecimal number to binary and reverse


Dr_Michael
August 5th, 1999, 08:13 AM
How can I convert a hexadecimal number to binary and the reverse? I went to:
www.planet-source-code.com/vb
but code is missing there :-(
Urgent plz!!!


Michael Vlastos
Company MODUS SA
Development Department
Athens, Greece
Tel: +3-01-9414900

Chris Eastwood
August 5th, 1999, 08:27 AM
Did you try :

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1474



Chris Eastwood

CodeGuru - the website for developers
http://www.codeguru.com/vb

Dr_Michael
August 5th, 1999, 08:56 AM
Nice try but didn't work:
The result (binary->hex) LOSES data. I mean that i lose some digits when the number is double!!!
I don't think is a bug of the code. The fault is at the data types (long, double etc.). Anyway, it didn't worked for my numbers :-( but thank you!

Michael Vlastos
Company MODUS SA
Development Department
Athens, Greece
Tel: +3-01-9414900

Mikesc
August 5th, 1999, 10:13 AM
I wrote a conversion from a Byte array once to see the bits of a monochrome bitmap in binary. I trashed the code but it went something like this:

Take the length in bits of the data type youre converting and set up a loop.

Inside the loop compare each bit of the number using the IIf function.

string1=IIf((number/2^n) & 1, "1", "0")
n=n+1
string2=string2 & string1

where n starts at 0 and loops up to total bits - 1
(or something like that)
Anyway it seems to me you could reverse the process and parse the string back into bits and dump them into a variant. Then just Hex the variant. I know Double and Single are stored internally in exponential format which may be causing your problem but wouldn't the variant automatically promote data types for you thereby solving the problem?
Just an idea...

Dr_Michael
August 6th, 1999, 05:09 AM
Your solution worked but I think the reverse doesn't. Thanx anyway. I cannot not to rate it...

Michael Vlastos
Company MODUS SA
Development Department
Athens, Greece
Tel: +3-01-9414900