我如何读十六进制数到C中的无符号整型 [英] How do I read hex numbers into an unsigned int in C

查看:198
本文介绍了我如何读十六进制数到C中的无符号整型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取文本文件十六进制数到一个无符号整数,这样我可以执行的机器指令。这只是一个模拟类型的事情,看起来该文本文件中,并根据价值和相应的指令在寄存器输出新的值。

例如,指令是:


  • 1RXY - >保存在值寄存器r
    内存地址XY

  • 2RXY - >保存与价值XY寄存器读

  • BRXY - >跳转到寄存器R,如果是XY
    这个那个等等。

  • ARXY - >并在值寄存器R
    内存地址XY

该文本文件包含在一个新行这样的事情每个。 (以十六进制)


  • 120F

  • B007

  • 290B

我的问题是复制每个指令到一个无符号整数......我该怎么做呢?

 的#include<&stdio.h中GT;
诠释主(){
    FILE * F;
    unsigned int类型NUM [80];    F =的fopen(values​​.txt,R);
    如果(F == NULL){
    的printf(文件犯规存在?!);
    }    INT I = 0;
    而(的fscanf(F,%X,NUM [I])!= EOF){
    的fscanf(F,%X,NUM [I]);
    我++;
    }
    FCLOSE(F);
    的printf(%X,NUM [0]);
}


解决方案

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


  • 您需要退出,如果 fopen()函数收益 NULL - 你打印一个错误消息,但随后持续

  • 您的循环应该结束,如果 I> = 80 ,所以你不读更多的整数比你有空间

  • 您需要 NUM [I] ,而不是价值,地址传递给的fscanf

  • 您正在呼叫的fscanf()两次循环,这意味着你扔掉你数值的一半,不保存它们。

下面是它看起来像那些修复的问题:

 的#include<&stdio.h中GT;诠释主(){
    FILE * F;
    unsigned int类型NUM [80];
    INT I = 0;
    INT RV;
    INT num_values​​;    F =的fopen(values​​.txt,R);
    如果(F == NULL){
        的printf(文件犯规存在\\ n吗?!);
        返回1;
    }    而(ⅰ&下; 80){
        RV =的fscanf(F,%X,试验#[I]);        如果(RV!= 1)
            打破;        我++;
    }
    FCLOSE(F);
    num_values​​ = I;    如果(ⅰ&GT = 80)
    {
        的printf(警告:停止读取输入,由于输入太长\\ n);
    }
    否则,如果(RV!= EOF)
    {
        的printf(警告:停止读取输入,由于恶劣的价值\\ n);
    }
    其他
    {
        的printf(到达输入结束\\ n);
    }    的printf(成功读取%d个值:\\ n,num_values​​);
    对于(i = 0; I< num_values​​;我++)
    {
        的printf(\\ t%X \\ n,NUM [I]);
    }    返回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]);
}

解决方案

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
}

这篇关于我如何读十六进制数到C中的无符号整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆