如何在 C 中将十六进制数字读入无符号整数

发布于 2024-08-05 00:46:06 字数 764 浏览 1 评论 0原文

我想将文本文件中的十六进制数字读入无符号整数,以便我可以执行机器指令。它只是一个模拟类型的东西,它查看文本文件内部并根据值及其相应的指令输出寄存器中的新值。

例如,指令为:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏至、离别 2024-08-12 00:46:06

你走在正确的轨道上。这是我看到的问题:

  • 如果 fopen() 返回 NULL,则需要退出 - 您正在打印错误消息,但然后继续。
  • 如果 i >= 80,循环应该终止,这样您就不会读取超出空间的整数。
  • 您需要将 num[i] 的地址(而不是值)传递给 fscanf
  • 您在循环中调用了两次 fscanf() ,这意味着您将丢弃一半的值而不存储它们。

以下是修复这些问题后的样子:

#include <stdio.h>

int main() {
    FILE *f;
    unsigned int num[80];
    int i=0;
    int rv;
    int num_values;

    f=fopen("values.txt","r");
    if (f==NULL){
        printf("file doesnt exist?!\n");
        return 1;
    }

    while (i < 80) {
        rv = fscanf(f, "%x", &num[i]);

        if (rv != 1)
            break;

        i++;
    }
    fclose(f);
    num_values = i;

    if (i >= 80)
    {
        printf("Warning: Stopped reading input due to input too long.\n");
    }
    else if (rv != EOF)
    {
        printf("Warning: Stopped reading input due to bad value.\n");
    }
    else
    {
        printf("Reached end of input.\n");
    }

    printf("Successfully read %d values:\n", num_values);
    for (i = 0; i < num_values; i++)
    {
        printf("\t%x\n", num[i]);
    }

    return 0
}

You're on the right track. Here's the problems I saw:

  • You need to exit if fopen() return NULL - you're printing an error message but then continuing.
  • Your loop should terminate if i >= 80, so you don't read more integers than you have space for.
  • You need to pass the address of num[i], not the value, to fscanf.
  • You're calling 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:

#include <stdio.h>

int main() {
    FILE *f;
    unsigned int num[80];
    int i=0;
    int rv;
    int num_values;

    f=fopen("values.txt","r");
    if (f==NULL){
        printf("file doesnt exist?!\n");
        return 1;
    }

    while (i < 80) {
        rv = fscanf(f, "%x", &num[i]);

        if (rv != 1)
            break;

        i++;
    }
    fclose(f);
    num_values = i;

    if (i >= 80)
    {
        printf("Warning: Stopped reading input due to input too long.\n");
    }
    else if (rv != EOF)
    {
        printf("Warning: Stopped reading input due to bad value.\n");
    }
    else
    {
        printf("Reached end of input.\n");
    }

    printf("Successfully read %d values:\n", num_values);
    for (i = 0; i < num_values; i++)
    {
        printf("\t%x\n", num[i]);
    }

    return 0
}
晨敛清荷 2024-08-12 00:46:06

您还可以使用函数 strtol()。如果您使用 16 为基数,它会将您的十六进制字符串值转换为 int/long。

errno = 0;
my_int = strtol(my_str, NULL, 16);
/* check errno */

编辑:另一点注意,各种静态分析工具可能会将 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.

errno = 0;
my_int = strtol(my_str, NULL, 16);
/* check errno */

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.

纸短情长 2024-08-12 00:46:06

您正在将两个数字读入数组的每个元素中(因此,当您覆盖它们时,您会丢失其中的一半。尝试仅

while (i < 80 && fscanf(f,"%x",&num[i]) != EOF)
    i++;

在循环中

使用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

while (i < 80 && fscanf(f,"%x",&num[i]) != EOF)
    i++;

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.

盛夏尉蓝 2024-08-12 00:46:06

在这种情况下,当您尝试读取小写十六进制时,您的所有输入都是大写十六进制

要修复此问题,请将 %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.

玩套路吗 2024-08-12 00:46:06

您是否希望每行(每行 4 个字符长)分隔在 4 个不同的数组元素中?如果你这样做,我会尝试这个:

/* read the line */
/* fgets(buf, ...) */

/* check it's correct, mind the '\n' */
/* ... strlen ... isxdigit ... */

/* put each separate input digit in a separate array member */
num[i++] = convert_xdigit_to_int(buf[j++]);

函数convert_xdigit_to_int()只是将'0'(字符)转换为0(整数),'1'转换为1,'2'转换为2,...'9'到 9,'a' 或 'A' 到 10,...

当然,伪代码位于一个循环内,该循环一直执行到文件用完或数组被填满为止。也许将 fgets() 作为条件一段时间(...)

while(/*there is space in the array && */ 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:

/* read the line */
/* fgets(buf, ...) */

/* check it's correct, mind the '\n' */
/* ... strlen ... isxdigit ... */

/* put each separate input digit in a separate array member */
num[i++] = convert_xdigit_to_int(buf[j++]);

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(...)

while(/*there is space in the array && */ fgets(...)) {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文