fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. constexpr const char * convert(char hx)
  7. {
  8. constexpr const char * hex[16] = {
  9. "0000","0001","0010","0011",
  10. "0100","0101","0110","0111",
  11. "1000","1001","1010","1011",
  12. "1100","1101","1110","1111" };
  13. if (hx >= '0' && hx <= '9') return hex[hx-'0'];
  14. else if (hx >= 'A' && hx <= 'F') return hex[hx-'A'+10];
  15. else if (hx >= 'a' && hx <= 'f') return hex[hx-'a'+10];
  16. return nullptr;
  17. }
  18.  
  19.  
  20.  
  21. int main(int argc, const char * argv[])
  22. {
  23. constexpr auto x = convert('F'); // x = "1111"
  24. cout << x << endl;
  25. char y = '1';
  26. const auto z = convert(y);
  27. cout << z << endl;
  28. }
  29.  
Success #stdin #stdout 0s 4376KB
stdin
Standard input is empty
stdout
1111
0001