How can I convert hexadecimal numbers to binary in C++?

后端 未结 6 409
星月不相逢
星月不相逢 2020-12-05 06:08

I am taking a beginning C++ class, and would like to convert letters between hex representations and binary. I can manage to print out the hex numbers using:



        
相关标签:
6条回答
  • 2020-12-05 06:14

    For bit of variety, you can also do it using a 16 element look up table.

    0 讨论(0)
  • 2020-12-05 06:25

    This sounds like an assignment, in which case you really should ask your teacher for help. Soliciting solutions to homework from the internet isn't really going to help you in the long run (unless you are going into project management).

    Answering chustar (the OQ'er) in the comments, I'd have to agree that if you understand how to do it, how/why it works,and how to figure it out yourself in the future, then yes, that would be a good thing.

    However, the answer he marked "correct" puts the lie to that argument. It contains nothing but code, prefaced with the words "Like so". It's pretty clear that what the OQ'er was looking for was not for an explanation, but for someone to write his code for him.

    0 讨论(0)
  • 2020-12-05 06:26

    There isn't a binary io manipulator in C++. You need to perform the coversion by hand, probably by using bitshift operators. The actual conversion isn't a difficult task so should be within the capabilities of a beginner at C++ (whereas the fact that it's not included in the standard library may not be :))

    Edit: A lot of others have put up examples, so I'm going to give my preferred method

    void OutputBinary(std::ostream& out, char character)
    {
      for (int i = sizeof(character) - 1; i >= 0; --i)
      {
        out << (character >> i) & 1;
      }
    }
    

    This could also be potentially templated to any numeric type.

    0 讨论(0)
  • 2020-12-05 06:29

    Like so:

    for(char c = 'a'; c <= 'z'; c++){
            std::bitset<sizeof(char) * CHAR_BIT> binary(c); //sizeof() returns bytes, not bits!
            std::cout << "Letter: " << c << "\t";
            std::cout << "Hex: " << std::hex << (int)c << "\t";
            std::cout << "Binary: " << binary << std::endl;
        }
    
    0 讨论(0)
  • 2020-12-05 06:31

    You can do something like this:

    for(char c = 'a'; c <= 'z'; c++){
        // char is 8 bits.  print 4 bits
        // at a time, starting with the MSB
        for (int i = 4; i>=0; i-=4) {
            switch (((int)c >> i) & 0xf) {
                case 0:
                    cout << "0000";
                    break;
                case 1:
                    cout << "0001";
                    break;
                .
                .
                .
                case 0xf:
                    cout << "1111";
                    break;
    
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:36

    You can easily write a mapping between the hex charachters an their binary 'nibbles':

    std::string HexCharToNibble( char c ) {
    switch (c) {
      case '0': return "0000";
      case '1': return "0001";
      //... fill in the rest
      case 'f': return "1111";
      default: assert(false); return "bad input";
    };
    
    0 讨论(0)
提交回复
热议问题