如何将整数中的十进制转换为整数十六进制?

时间:2013-05-23 23:26:32

标签: c++ c binary hex

cout<<std::hex<<dec;

我想以0x ...

的形式将它存储到int中

如何将该值存储为整数而不是将其打印出来?

2 个答案:

答案 0 :(得分:2)

如果您有一个整数值,并且想要打印它,只需执行以下操作(在C中):

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

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

你不能忘记,对于一台机器,只有0和1。 您只需要定义打印方式

在C ++中:

int number = 555;
std::cout << std::hex << number << std::endl; //this will print the number in hexadecimal

答案 1 :(得分:2)

您可以先将值存储到字符串流中:

#include <stringstream>

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

int n;
ss >> n;