使用C中的fscanf读取一系列HEX值(每次2个)

所以我有一个文本文件,假设名为text.txt。 其格式为: 4C 4D 4E 4F (即字符串“LMNO”的hex值)。 我已正确打开文本文件等。我应该如何使用fscanf读取四个字节的序列(即一次1个字节),将每个字节存储在一个变量中(具有适当的数据类型)。 我的最终目标是了解如何在字符数组中存储char test[4]; 字母LMNO (即test [0] =’L’,test [1] =’M’,…)

 #include int main(void){ FILE *fp = fopen("text.txt", "r"); char test[5] = {0}; unsigned hex; int i; for(i=0; i < 4; ++i){ if(1==fscanf(fp, "%2x", &hex)) test[i] = (char)hex; else break; } fclose(fp); puts(test); return 0; }