char类型占一个字节,8位
int类型四个字节32位
(long类型的转换跟int类型相同)
#include <stdio.h> int IntToChar(int tmp, char buffer[4]) { buffer[0] = (char)tmp; buffer[1] = (char)(tmp >> 8); buffer[2] = (char)(tmp >> 16); buffer[3] = (char)(tmp >> 24); return 0; } int main() { unsigned int tmp = 0x18345679; char buffer[4] = {0}; int i = 0; IntToChar(tmp, buffer); for(i = 0; i < 4;i++) { printf("buffer[%d] = %02x\n",i, buffer[i]); } getchar(); return 0; }
所得结果:
时间: 2024-10-10 21:48:57