fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <sstream>
  5. #include <climits>
  6. #include <type_traits>
  7. #include <cstdint>
  8.  
  9. // -----
  10.  
  11. // First option, with `<< "0x"`.
  12. namespace ZeroX {
  13. namespace detail {
  14. constexpr int HEX_DIGIT_BITS = 4;
  15.  
  16. template<typename T> struct is_char
  17. : std::integral_constant<bool,
  18. std::is_same<T, char>::value ||
  19. std::is_same<T, signed char>::value ||
  20. std::is_same<T, unsigned char>::value> {};
  21. }
  22.  
  23. template<typename T>
  24. std::string hex_out_s(T val) {
  25. using namespace detail;
  26.  
  27. std::stringstream sformatter;
  28. sformatter << std::hex
  29. << std::internal
  30. << "0x"
  31. << std::setfill('0')
  32. << std::setw(sizeof(T) * CHAR_BIT / HEX_DIGIT_BITS)
  33. << (is_char<T>::value ? static_cast<int>(val) : val);
  34.  
  35. return sformatter.str();
  36. }
  37. } // ZeroX
  38.  
  39. // -----
  40.  
  41. // Second option, with `<< std::showbase`.
  42. namespace ShowBase {
  43. namespace detail {
  44. constexpr int HEX_DIGIT_BITS = 4;
  45. constexpr int HEX_BASE_CHARS = 2;
  46.  
  47. template<typename T> struct is_char
  48. : std::integral_constant<bool,
  49. std::is_same<T, char>::value ||
  50. std::is_same<T, signed char>::value ||
  51. std::is_same<T, unsigned char>::value> {};
  52. }
  53.  
  54. template<typename T>
  55. std::string hex_out_s(T val) {
  56. using namespace detail;
  57.  
  58. std::stringstream sformatter;
  59. sformatter << std::hex
  60. << std::internal
  61. << std::showbase
  62. << std::setfill('0')
  63. << std::setw((sizeof(T) * CHAR_BIT / HEX_DIGIT_BITS) + HEX_BASE_CHARS)
  64. << (is_char<T>::value ? static_cast<int>(val) : val);
  65.  
  66. return sformatter.str();
  67. }
  68. } // ShowBase
  69.  
  70. // -----
  71.  
  72. int main() {
  73. uint32_t hexU32 = 0x0f;
  74. int hexI = 0x3c;
  75. unsigned short hexUS = 0x12;
  76. char hexC = 0xf;
  77. unsigned char hexUC = 0xa;
  78. signed char hexSC = 0x3;
  79.  
  80. std::cout << std::left
  81. << std::setw(22) << "uint32_t:"
  82. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(hexU32)
  83. << std::setw(23) << '\n'
  84. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(hexU32)
  85.  
  86. << std::setw(24) << "\n\nint:"
  87. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(hexI)
  88. << std::setw(23) << '\n'
  89. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(hexI)
  90.  
  91. << std::setw(24) << "\n\nunsigned short:"
  92. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(hexUS)
  93. << std::setw(23) << '\n'
  94. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(hexUS)
  95.  
  96. << std::setw(24) << "\n\nchar:"
  97. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(hexC)
  98. << std::setw(23) << '\n'
  99. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(hexC)
  100.  
  101. << std::setw(24) << "\n\nunsigned char:"
  102. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(hexUC)
  103. << std::setw(23) << '\n'
  104. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(hexUC)
  105.  
  106. << std::setw(24) << "\n\nsigned char:"
  107. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(hexSC)
  108. << std::setw(23) << '\n'
  109. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(hexSC)
  110.  
  111. << std::setw(24) << "\n\nDecimal rvalues, too:"
  112. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(29)
  113. << std::setw(23) << '\n'
  114. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(29)
  115.  
  116. << std::setw(24) << "\n\nA long long:"
  117. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(LLONG_MAX)
  118. << std::setw(23) << '\n'
  119. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(LLONG_MAX)
  120.  
  121. << std::setw(24) << "\n\nAnd zero:"
  122. << std::setw(18) << "0x version: " << ZeroX::hex_out_s(0)
  123. << std::setw(23) << '\n'
  124. << std::setw(18) << "showbase version: " << ShowBase::hex_out_s(0)
  125. << "\n\n" << std::endl;
  126. }
Success #stdin #stdout 0s 16088KB
stdin
Standard input is empty
stdout
uint32_t:             0x version:       0x0000000f
                      showbase version: 0x0000000f

int:                  0x version:       0x0000003c
                      showbase version: 0x0000003c

unsigned short:       0x version:       0x0012
                      showbase version: 0x0012

char:                 0x version:       0x0f
                      showbase version: 0x0f

unsigned char:        0x version:       0x0a
                      showbase version: 0x0a

signed char:          0x version:       0x03
                      showbase version: 0x03

Decimal rvalues, too: 0x version:       0x0000001d
                      showbase version: 0x0000001d

A long long:          0x version:       0x7fffffffffffffff
                      showbase version: 0x7fffffffffffffff

And zero:             0x version:       0x00000000
                      showbase version: 0000000000