How do you convert decimal in integer to hexadecim

2020-05-10 10:29发布

cout<<std::hex<<dec;

I want to store it to an int in the form of 0x...

How do I store that value in an integer instead of printing it out?

标签: c++ c binary hex
2条回答
叛逆
2楼-- · 2020-05-10 10:42

If you have an integer value, and you want to print it just do the following (in C):

int number = 555;
printf("%d",number); //this prints number in decimal

printf("%x",number); //this prints number in haxadecimal

You must not forget, to a machine, there are only 0's and 1's. You just have to define the way you want to print it

In C++:

int number = 555;
std::cout << std::hex << number << std::endl; //this will print the number in hexadecimal
查看更多
可以哭但决不认输i
3楼-- · 2020-05-10 10:50

You can store the value into a string stream first:

#include <stringstream>

std::stringstream ss;
ss << std::hex << dec;

int n;
ss >> n;
查看更多
登录 后发表回答