如何在 C 中将十六进制数字读入无符号整数
我想将文本文件中的十六进制数字读入无符号整数,以便我可以执行机器指令。它只是一个模拟类型的东西,它查看文本文件内部并根据值及其相应的指令输出寄存器中的新值。
例如,指令为:
- 1RXY -> 1RXY ->将寄存器 R 的值保存在 内存地址 XY
- 2RXY ->将值 XY
- BRXY 保存到寄存器 R ->如果 xy 为,则跳转到寄存器 R 这个那个等等..
- ARXY -> AND 寄存器 R 的值为 内存地址 XY
文本文件在新行中包含类似的内容。 (十六进制)
- 120F
- B007
- 290B
我的问题是将每个单独的指令复制到无符号整数中...我该怎么做?
#include <stdio.h>
int main(){
FILE *f;
unsigned int num[80];
f=fopen("values.txt","r");
if (f==NULL){
printf("file doesnt exist?!");
}
int i=0;
while (fscanf(f,"%x",num[i]) != EOF){
fscanf(f,"%x",num[i]);
i++;
}
fclose(f);
printf("%x",num[0]);
}
I'm wanting to read hex numbers from a text file into an unsigned integer so that I can execute Machine instructions. It's just a simulation type thing that looks inside the text file and according to the values and its corresponding instruction outputs the new values in the registers.
For example, the instructions would be:
- 1RXY -> Save register R with value in
memory address XY - 2RXY -> Save register R with value XY
- BRXY -> Jump to register R if xy is
this and that etc.. - ARXY -> AND register R with value at
memory address XY
The text file contains something like this each in a new line. (in hexidecimal)
- 120F
- B007
- 290B
My problem is copying each individual instruction into an unsigned integer...how do I do this?
#include <stdio.h>
int main(){
FILE *f;
unsigned int num[80];
f=fopen("values.txt","r");
if (f==NULL){
printf("file doesnt exist?!");
}
int i=0;
while (fscanf(f,"%x",num[i]) != EOF){
fscanf(f,"%x",num[i]);
i++;
}
fclose(f);
printf("%x",num[0]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你走在正确的轨道上。这是我看到的问题:
fopen()
返回NULL
,则需要退出 - 您正在打印错误消息,但然后继续。i >= 80
,循环应该终止,这样您就不会读取超出空间的整数。num[i]
的地址(而不是值)传递给fscanf
。fscanf()
,这意味着您将丢弃一半的值而不存储它们。以下是修复这些问题后的样子:
You're on the right track. Here's the problems I saw:
fopen()
returnNULL
- you're printing an error message but then continuing.i >= 80
, so you don't read more integers than you have space for.num[i]
, not the value, tofscanf
.fscanf()
twice in the loop, which means you're throwing away half of your values without storing them.Here's what it looks like with those issues fixed:
您还可以使用函数 strtol()。如果您使用 16 为基数,它会将您的十六进制字符串值转换为 int/long。
编辑:另一点注意,各种静态分析工具可能会将 atoi() 和 scanf() 等标记为不安全。 atoi 已过时,因为它不像 strtol() 那样检查错误。另一方面,scanf() 可能会造成某种缓冲区溢出,因为它不检查发送到 scanf() 的类型。例如,您可以向 scanf 提供一个指向short的指针,其中读取的值实际上是一个long......并且繁荣。
You can also use the function strtol(). If you use a base of 16 it will convert your hex string value to an int/long.
Edit: One other note, various static analysis tools may flag things like atoi() and scanf() as unsafe. atoi is obsolete due to the fact that it does not check for errors like strtol() does. scanf() on the other hand can do a buffer overflow of sorts since its not checking the type sent into scanf(). For instance you could give a pointer to a short to scanf where the read value is actually a long....and boom.
您正在将两个数字读入数组的每个元素中(因此,当您覆盖它们时,您会丢失其中的一半。尝试仅
在循环中
使用edit ,
您还缺少 '&' 来获取数组元素的地址,因此您将随机垃圾指针传递给 scanf 并且可能会崩溃 -Wall 选项是您的朋友。
You're reading two numbers into each element of your array (so you lose half of them as you overwrite them. Try using just
for your loop
edit
you're also missing the '&' to get the address of the array element, so you're passing a random garbage pointer to scanf and probably crashing. The -Wall option is your friend.
在这种情况下,当您尝试读取小写十六进制时,您的所有输入都是大写十六进制。
要修复此问题,请将
%x
更改为%X
。In this case, all of your input is upper case hex while you are trying to read lower case hex.
To fix it, change
%x
to%X
.您是否希望每行(每行 4 个字符长)分隔在 4 个不同的数组元素中?如果你这样做,我会尝试这个:
函数convert_xdigit_to_int()只是将'0'(字符)转换为0(整数),'1'转换为1,'2'转换为2,...'9'到 9,'a' 或 'A' 到 10,...
当然,伪代码位于一个循环内,该循环一直执行到文件用完或数组被填满为止。也许将 fgets() 作为条件一段时间(...)
Do you want each of the lines (each 4 characters long) separated in 4 different array elements? If you do, I'd try this:
Where the function convert_xdigit_to_int() simply converts '0' (the character) to 0 (an int), '1' to 1, '2' to 2, ... '9' to 9, 'a' or 'A' to 10, ...
Of course that pseudo-code is inside a loop that executes until the file runs out or the array gets filled. Maybe putting the fgets() as the condition for a while(...)