8个十六进制数存在一个数组里
Arry[8]= {0x00,0x00.,0x00,0x03,0x0F,0x93,0x2E,0xBE};
转换成一个unsigned long long型的整数中,a= 0x000000030F932EBE
下面的程序不知道哪儿出了问题,得出的数据是0,求指点。
void HEXtoLongLong( unsigned char * byteArry,unsigned long long x )
{
u8 i;
unsigned char* p = (unsigned char*) &x;
for ( i =0; i < 8; i++ )
{
*p = *byteArry;
byteArry++;
p++;
}
}
Arry[8]= {0x00,0x00.,0x00,0x03,0x0F,0x93,0x2E,0xBE};
转换成一个unsigned long long型的整数中,a= 0x000000030F932EBE
下面的程序不知道哪儿出了问题,得出的数据是0,求指点。
void HEXtoLongLong( unsigned char * byteArry,unsigned long long x )
{
u8 i;
unsigned char* p = (unsigned char*) &x;
for ( i =0; i < 8; i++ )
{
*p = *byteArry;
byteArry++;
p++;
}
}
解决方案
20
#include <stdio.h> typedef unsigned char u8; //8个十六进制数存在一个数组里 unsigned char Arry[8]= {0x00,0x00,0x00,0x03,0x0F,0x93,0x2E,0xBE}; //转换成一个unsigned long long型的整数中,a= 0x000000030F932EBE unsigned long long a; void HEXtoLongLongBE(unsigned char *byteArry,unsigned long long *x) { u8 i; unsigned char* p = (unsigned char*)x; for ( i =0; i < 8; i++ ) { *p = *byteArry; byteArry++; p++; } } void HEXtoLongLongLE(unsigned char *byteArry,unsigned long long *x) { u8 i; unsigned char* p = (unsigned char*)x; byteArry+=7; for ( i =0; i < 8; i++ ) { *p = *byteArry; byteArry--; p++; } } int main() { HEXtoLongLongBE(Arry,&a); printf("0x%016llx\n",a); HEXtoLongLongLE(Arry,&a); printf("0x%016llx\n",a); return 0; } //0xbe2e930f03000000 //0x000000030f932ebe //