简体   繁体   中英

Representing N bit int as hex in c++

I've a number between 0 and 63, so max 6 bits.
Then I've to represent this number in hexadecimal.

How can I force the reresentation of my number in 8 bits so that if I have:

int x = 60;
cout << std::hex << x; 

it prints 0x3C ?

try this :

#include<iostream>
#include <iomanip>
using namespace std;

}
int main(){
    int x = 60;
    cout<<showbase; // show the 0x prefix
    cout<<hex<<x; 
    return 0;
}

output :

0x3c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM