C++/Data Type/Hexadecimal
Содержание
- 1 Display hexadecimal integer literals and decimal integer literals.
- 2 Display hexadecimal value as decimal value
- 3 Enter hexadecimal digits and a floating-point number
- 4 gain access to cout, cin, and hex
- 5 Output digits in hex and oct
- 6 Output Hexadecimal default
- 7 Output Hexadecimal with lowercase base
- 8 Output Hexadecimal without base
- 9 Output Hexadecimal with uppercase base
- 10 Set cout to output hex number
Display hexadecimal integer literals and decimal integer literals.
#include <iostream>
using namespace std;
int main()
{
cout << "Value of 0xFF = " << 0xFF << " decimal" << endl;
cout << "Value of 27 = " << hex << 27 <<" hexadecimal" << endl;
return 0;
}
Display hexadecimal value as decimal value
#include <iostream>
using namespace std;
int main(void){
cout << "Decimal value " << dec << 0xFF;
cout << "\nOctal value " << oct << 10;
cout << "\nHexadecimal value " << hex << 255;
}
Enter hexadecimal digits and a floating-point number
#include <iostream>
#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;
}
gain access to cout, cin, and hex
#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;
}
Output digits in hex and oct
#include <iostream>
#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;
}
Output Hexadecimal default
#include <iomanip>
#include <iostream>
using namespace std;
int main( )
{
cout << "Enter a hexadecimal number: ";
int num;
cin >> hex >> num;
cout << "\nDecimal: " << num
<< "\nHexadecimal default: " << hex << num;
}
Output Hexadecimal with lowercase base
#include <iomanip>
#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;
}
Output Hexadecimal without base
#include <iomanip>
#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;
}
Output Hexadecimal with uppercase base
#include <iomanip>
#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;
}
Set cout to output hex number
#include <iostream>
#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;
}