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

Reading a sequence of HEX value (2 at a time) using fscanf in C

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

#include<stdio.h>

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;
}