Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read hex numbers into an unsigned int in C

Tags:

c

file

copying

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]);
}
like image 957
silent Avatar asked Dec 05 '22 05:12

silent


2 Answers

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
}
like image 192
caf Avatar answered Dec 07 '22 19:12

caf


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.

like image 41
KFro Avatar answered Dec 07 '22 19:12

KFro