C ++ cout conversion format output (octal and hexadecimal), cout hexadecimal

Source: Internet
Author: User

C ++ cout conversion format output (octal and hexadecimal), cout hexadecimal

// Hex, oct output # include <iostream> using namespace std; int main () {int chest = 42, waist = 42, inseam = 42; cout <"chest =" <chest <"(decimal for 42)" <endl; cout 


How to convert hexadecimal to octal in C ++

The computer is in binary mode, and the input and output are only reflected in other hexadecimal modes.

# Include <iostream>
# Include <iomanip>
Using namespace std;
Main (){
Int x;
Cout <"input hex data" <endl;
Cin> hex> x; // The number of inputs in this method is in hexadecimal notation.
Cout <oct <x <endl; // The number output by this method is in octal format.
}

By the way, the input/output % x format in C language is hexadecimal and % o is octal.

The user enters a decimal positive integer and converts it to binary, octal, and hexadecimal output (C ++, not C)

# Include <iostream>
Using namespace std;
Int main ()
{
Int a, B, c, d;
Cout <"Enter the number to be converted :";
Cin>;
// Calculate each bit in binary format
If (a = 0) cout <;
B = a; c = 0; d = 1;
While (B! = 0)
{
C + = (B % 2) * d; // write the remainder of B divided by 2 in sequence at the corresponding position of c.
B = B/2;
D * = 10;
}
Cout <c <endl;
// The octal and hex formats are formatted and output. oct indicates octal and hex indicates hexadecimal.
Cout <oct <a <endl;
Cout }

If you have any questions, contact us.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.