我想知道是否有人可以帮助我我只学习c,我试图将unsigned char数组的内容分成两半,结果可以存储在两个unsigned int中,
出于示例目的,我在下面有一些代码将十六进制值添加到BYTE数组中,那么如何将val []的内容分成两个但保持相同的顺序
#include <stdio.h>
typedef unsigned char BYTE;
int main()
{
// Sample purposes putting hex into val[8]
int i,j;
long long hex=0x78661EB54FE76763;
BYTE val[8];
for(j=0,i=7; i>=0; i--,j++){
val[j]= (hex>>(i*8))&0xff;
}
// How to split the contents of val[8] which now holds the hex
return 0;
}
我正在尝试将十六进制值拆分为78661EB5,4FE76763,并将每个值存储在unsigned int中,该值存储在我的示例中的val [8]中
答案 0 :(得分:0)
创建一个辅助函数,该函数获取指向unsigned char
数组的指针并调用它两次。
留下一些东西让OP发现。
unsigned long form_big_endian4(const unsigned char * x) {
unsigned long y = TBB; // What should the initial value be?
for (int i = 0; i < TBD; i++) { // How many times to loop?
y *= TBD; // Hint: max value of 8 bits + 1
y += x[i];
}
return y;
}
#include <stdio.h>
int main(void) {
const unsigned char val[8] =
{ 0x78, 0x66, 0x1E, 0xB5, 0x4F, 0xE7, 0x67, 0x63 };
unsigned long half = form_big_endian4(val); // Why long: Hint how small can unsigned be?
printf("%08lX\n", half); // Why 0 in format? Why `lX`?
half = form_big_endian4(val + TBD); // How far an offset?
printf("%08lX\n", half);
return 0;
}