C++/Data Type/Hexadecimal

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Display hexadecimal integer literals and decimal integer literals.

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

 cout << "Value of 0xFF = " << 0xFF << " decimal" << endl;                 
 cout << "Value of 27 = " << hex << 27 <<" hexadecimal" << endl;              
 return 0;

}


 </source>


Display hexadecimal value as decimal value

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void){

  cout << "Decimal value " << dec << 0xFF;
  cout << "\nOctal value " << oct << 10;
  cout << "\nHexadecimal value " << hex << 255;

}


 </source>


Enter hexadecimal digits and a floating-point number

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; int main() {

  int number = 0;
  cout << "Enter a hexadecimal number: " << endl;
  cin >> hex >> number;     
  cout << "A decimal input: " << number << endl;
  cin.sync();                     // Clears the buffer
  cin.clear();                    // Reset error flags
  double x1 = 0.0, x2 = 0.0;
  cout << "Now enter two floating-point values: " << endl;
  cout << "1. floating-point number: ";
  cin  >> x1;                     // Read first number
  cout << "2. floating-point number: ";
  cin  >> x2;                     // Read second number
  cout << fixed << setprecision(2)
       << "The sum of both numbers:   "
       << endl  << setw(10) << x1 + x2 << endl;
  cout << "The product of both numbers: "
        << endl << setw(10) << x1 * x2 << endl;
  return 0;

}


 </source>


gain access to cout, cin, and hex

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::cin; using std::hex; int main() {

 int val;
 cout << "Enter a number: ";
 cin >> val;
 cout << "This is your number: ";
 cout << hex << val;
 return 0;

}


 </source>


Output digits in hex and oct

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; int main() {

 cout << hex << 100 << endl;
 cout << oct << 10 << endl;
 cout << setfill("X") << setw(10);
 cout << 100 << " hi " << endl;
 return 0;

}


 </source>


Output Hexadecimal default

<source lang="cpp">

  1. include <iomanip>
  2. include <iostream>

using namespace std; int main( ) {

  cout << "Enter a hexadecimal number: ";
  int num;
  cin >> hex >> num;
  cout << "\nDecimal: " << num
       << "\nHexadecimal default: " << hex << num;

}


 </source>


Output Hexadecimal with lowercase base

<source lang="cpp">

  1. include <iomanip>
  2. include <iostream>

using namespace std; int main( ) {

  cout << "Enter a hexadecimal number: ";
  int num;
  cin >> hex >> num;
  cout << "\nDecimal: " << num
       << "\nHexadecimal with lowercase base: " << nouppercase << num;

}


 </source>


Output Hexadecimal without base

<source lang="cpp">

  1. include <iomanip>
  2. include <iostream>

using namespace std; int main( ) {

  cout << "Enter a hexadecimal number: ";
  int num;
  cin >> hex >> num;
  cout << "\nDecimal: " << num
       << "\nHexadecimal without base: " << noshowbase << num;

}


 </source>


Output Hexadecimal with uppercase base

<source lang="cpp">

  1. include <iomanip>
  2. include <iostream>

using namespace std; int main( ) {

  cout << "Enter a hexadecimal number: ";
  int num;
  cin >> hex >> num;
  cout << "\nDecimal: " << num
       << "\nHexadecimal with uppercase base: " << showbase << uppercase << num;

}


 </source>


Set cout to output hex number

<source lang="cpp">

  1. include <iostream>
  2. include <iomanip>

using namespace std; int main() {

  const int number = 185;
  cout << "The number is " << number << endl;
  cout << "The number is " << hex <<  number << endl;
  cout.setf(ios::showbase);
  cout << "The number is " << hex <<  number << endl;
  cout << "The number is " ;
  cout.width(10);
  cout << hex << number << endl;
  cout << "The number is " ;
  cout.width(10);
  cout.setf(ios::left);
  cout << hex << number << endl;
  cout << "The number is " ;
  cout.width(10);
  cout.setf(ios::internal);
  cout << hex << number << endl;
  cout << "The number is " << setw(10) << hex << number << endl;
  return 0;

}


 </source>