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

李国帅 取自日志 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
*/

猜你喜欢

转载自blog.csdn.net/lgs790709/article/details/79484923