0
点赞
收藏
分享

微信扫一扫

使用cout输出16进制数和10进制数

E_topia 2022-07-18 阅读 26


李国帅 取自日志 2007-9-12 15:36

这是按照机器代码的存取,是小字节在前的顺序。


#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <conio.h>

#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0x1234abcd;
char c = (char)i; //cd
cout << i << endl;//10进制

cout.unsetf(ios_base::dec);
cout.setf(ios_base::hex);
cout << i << endl;//16进制

unsigned char* pc = (unsigned char*)&i;//把i的地址变成一个指向char*的指针
cout << (int)*pc << " " << (int)*(pc + 1) << " " << (int)*(pc + 2) << " " << (int)*(pc + 3) << " " << endl;

cout.setf(ios_base::dec);
cout << i << endl;//10进制
cout.setf(ios_base::hex, ios_base::dec);
cout << i << endl;//16进制

getchar();
return 0;
}
/*
* pc 0xcd char
*(pc+1) 0xab char
*(pc+2) 0x34 '4' char
*(pc+3) 0x12 '?' char
- &i 0x0013fed4 int *
0x1234abcd int
*/

举报

相关推荐

0 条评论