CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Convert hexadecimal number to binary and reverse

    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

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Convert hexadecimal number to binary and reverse

    Did you try :

    http://www.planet-source-code.com/vb...txtCodeId=1474



    Chris Eastwood

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

  3. #3
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Convert hexadecimal number to binary and reverse

    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

  4. #4
    Join Date
    Jul 1999
    Posts
    145

    Re: Convert hexadecimal number to binary and reverse

    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...


  5. #5
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Convert hexadecimal number to binary and reverse

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured