Hexadecimal to Binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjbauer95
    New Member
    • Jan 2009
    • 13

    Hexadecimal to Binary

    Are there any functions out there to convert Hex to Bin?

    Like if I had 0xA0 it would convert it to 0b10100000.

    But I mainly want this so I could get the say the first bit like:
    Code:
    bool getHex(char * hex, int place);
    And I could write getHex(0xA0, 16) and it would return true.
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    Try using the 'binary AND'-operator (&)

    Example:
    [code=c++]#include <iostream>
    using namespace std;

    int main() {
    short val1 = 53; //00110101
    short val2 = 4; //00000100
    short val3 = val1&val2;
    cout << val3;
    return 0;
    }

    /* gives '4' as output because the 3rd bit of val1 is
    '1', if it were '0', then the 3rd bit would of course be
    '0' */[/code]
    This example is written in C++, but if you use C, it shouldn't be too different.

    Comment

    • mjbauer95
      New Member
      • Jan 2009
      • 13

      #3
      Thanks I knew it was something like that but it's nice to have the bytes.com community's help here on stuff that is really hard to find on Google.

      Comment

      • MrPickle
        New Member
        • Jul 2008
        • 100

        #4
        You can always use a bitset if you want to convert something into binary.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          A book I once read by Jesse Liberty and David B Horvath,CCP pointed out that
          for any binary number e.g.10100011111 10001
          break it up into groups of 4 integers from RH end so:
          bin 1010 0011 1111 0001 then put the hex symbol under each:
          (dec) (10) (3) (15) (1)
          hex A 3 F 1
          result is 101000111111000 1 = A3F1 = 41,969
          It will also work in the reverse direction.
          The LH group may of course have < 4 binary symbols.
          Hope this helps.

          Comment

          • YarrOfDoom
            Recognized Expert Top Contributor
            • Aug 2007
            • 1243

            #6
            I feel I should point out that you're breaking them up in bits, not integers. An integer is a number (not floating point), saved in a set of 4 bytes (1 byte = 8 bits). Actually, depending on the compiler you're using, it may be more or less than 4 bytes, you can look it this up by doing sizeof(int). This will return the number of bytes used by an integer.

            Comment

            • MrPickle
              New Member
              • Jul 2008
              • 100

              #7
              Originally posted by YarrOfDoom
              (1 byte = 8 bits).
              This is not always true, it can vary according to OS and hardware.

              Comment

              • YarrOfDoom
                Recognized Expert Top Contributor
                • Aug 2007
                • 1243

                #8
                Well yes, I should have thought of that too.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by YarrOfDoom
                  Well yes, I should have thought of that too.
                  You should've written 'octet'; it sounds more elegant. Or even 'octette' (the female form) would make it sound even a bit sexy ;-)

                  kind regards,

                  Jos

                  Comment

                  Working...