// ByteOrder_demo.cpp : 定義控制臺應用程序的入口點。#include"stdafx.h"#include<iostream>usingnamespace std;voidJdugeByteOrder(){int a =0x12345678;unsignedchar*p =(unsignedchar*)&a;for(int i =0; i <sizeof(int);i++){int b =*(p+i);cout<<hex<<b<<"\t";/*cout<<*(p+i)<<"\t";*//這樣無法正確輸出,不知道為什么,望知情者不吝賜教}cout<<endl;}int_tmain(int argc, _TCHAR* argv[]){JdugeByteOrder();system("pause");return0;}
輸出結果:
示例二
// ByteOrder_demo.cpp : 定義控制臺應用程序的入口點#include"stdafx.h"#include<iostream>usingnamespace std;voidJdugeByteOrder(){unsignedshort b =2;unsignedchar* p =(unsignedchar*)&b;for(int i =0; i<sizeof(unsignedshort);i++){printf("%x\n",*(p+i));}cout<<endl;}int_tmain(int argc, _TCHAR* argv[]){JdugeByteOrder();system("pause");return0;}