Advertisement
avr39ripe

cppDecToHexVariants

Feb 16th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     int decNum{ 17 };
  6.     int hexDigit{ 0 };
  7.     char hexH{ '0' };
  8.     char hexL{ '0' };
  9.  
  10.     hexDigit = decNum % 16;
  11.    
  12.     //(hexDigit < 10) and (hexL = '0' + hexDigit);
  13.     //(hexDigit >= 10) and (hexL = 'A' + (hexDigit - 10));
  14.     //(hexDigit < 10) and (hexL = '0' + hexDigit) or (hexL = 'A' + (hexDigit - 10));
  15.  
  16.     if (hexDigit < 10) { hexL = '0' + hexDigit; }
  17.     else { hexL = 'A' + (hexDigit - 10); };
  18.  
  19.     decNum /= 16;
  20.     hexDigit = decNum % 16;
  21.  
  22.     //(hexDigit < 10) and (hexH = '0' + hexDigit);
  23.     //(hexDigit >= 10) and (hexH = 'A' + (hexDigit - 10));
  24.     //(hexDigit < 10) and (hexH = '0' + hexDigit) or (hexH = 'A' + (hexDigit - 10));
  25.     if (hexDigit < 10) { hexH = '0' + hexDigit; }
  26.     else { hexH = 'A' + (hexDigit - 10); };
  27.  
  28.     std::cout << hexH << hexL << '\n';
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement