[英]Fscanf: Reading Digits seperatly in Groups of 4
我有一个包含以 4 位数字块编码的十六进制数据的文件。
400F
4101
6010
1000
我想在 4 个块中分别读取每个数字。例如:读取 400F 并将它们保存在单独的无符号短裤中。 我只能用空格分隔数字来做到这一点:
uint16_t test[4];
fscanf(file, "%hx%hx%hx%hx", &test[0], &test[1], &test[2], &test[3])
它应该是这样的:
test[0] -> 4
test[1] -> 0
test[2] -> 0
test[3] -> 15
(所有这些当然都在一个循环中,以获得不止一行)
谁能帮我这个? 谢谢:)
使用%x
将其读入 32 位数字,然后使用移位和掩码提取每个数字。
uint32_t input;
uint16_t test[4];
fscanf(file, "%x", input);
test[0] = (input >> 24) & 0xf;
test[1] = (input >> 16) & 0xf;
test[2] = (input >> 8) & 0xf;
test[3] = input & 0xf;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.