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

后端 未结 6 425
星月不相逢
星月不相逢 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: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";
    };
    

提交回复
热议问题