
- 加为好友
- 发送私信
- 在线聊天
|
| 发表于:2008-05-09 13:57:5831楼 得分:5 |
- C/C++ code
int b = 0x30313233; // 0x30 is ascii for '0', and so on
char a = (char)b; // cast from int to char: get the "last" byte 0x33
// i.e. b & 0xff
char a = *(char*)&b; // get the byte pointed by the "&b"
// cast to make "&b" a char pointer
bin-endian:
base_address + 3: |0x33|
base_address + 2: |0x32|
base_address + 1: |0x31|
base_address + 0: |0x30| <-- &b point here
little-endian:
base_address + 3: |0x30|
base_address + 2: |0x31|
base_address + 1: |0x32|
base_address + 0: |0x33| <-- &b point here
default, &b is a pointer of int and 4 bytes will be interpreted;
by casting &b into a pointer of char, only 1 byte is interpreted.
- C/C++ code
#include <stdio.h>
typedef unsigned char BYTE;
void print_object(const BYTE *base, int size)
{
printf("object is at %p\n", base);
printf(" address content ascii\n");
while(size > 0)
{
--size;
printf("base_address + %d: %p | 0x%02X | %c\n",
size,
base + size,
*
| |