回复 5楼 afanihao
兄台,你这个整数定义下草率了吧
莫问前尘有愧,但求今生无悔
#include <stdio.h> #include <stdlib.h> #include <conio.h> int main(void) { const int n = 32164143; // 源數 printf_s("1.十進制表示: %d\n", n); printf_s("2.十六進制表示: %X\n", n); printf_s("3.數據在內存中的映像: "); unsigned char* p = (unsigned char*)&n; // 用指針指向變量n的入口地址 for (int i = 0; i < sizeof(n); ++i) { printf_s("%02X ", *p++); } putchar('\n'); printf_s("4.取出數據的數組: "); int arr[sizeof(n)]; p = (unsigned char*)&n; // 復位指針到n的入口處 for (int i = 0; i < sizeof(n); ++i) { arr[i] = *p++; printf_s("%02X ", arr[i]); } printf_s("\nPress any key to continue..."); _getch(); return EXIT_SUCCESS; }
#include <stdio.h> #include <stdlib.h> #include <conio.h> int main(void) { const int n = 32164143; // 源數 printf_s("1.十進制表示: %d\n", n); printf_s("2.十六進制表示: %X\n", n); printf_s("3.數據在內存中的映像: "); unsigned char* p = (unsigned char*)&n; // 用指針指向變量n的入口地址 for (int i = 0; i < sizeof(n); ++i) { printf_s("%02X ", *p++); } putchar('\n'); printf_s("4.取出數據的數組: "); int arr[sizeof(n)]; p = (unsigned char*)&n; // 復位指針到n的入口處 for (int i = 0; i < sizeof(n); ++i) { arr[i] = *p++; printf_s("%02X ", arr[i]); } putchar('\n'); printf_s("5.把數組反演爲整數: "); int x = 0; for (int i = 0; i < _countof(arr); ++i) { x += arr[i] << (i * 8); // 警告:這個算法取決於機器! } printf_s("%d\n", x); printf_s("\nPress any key to continue..."); _getch(); return EXIT_SUCCESS; }