How to read hexadecimal numbers in unsigned int in C

I want to read hexadecimal numbers from a text file into an unsigned integer so that I can follow machine instructions. It is simply a type of simulation type that scans a text file and according to the values, and the corresponding command outputs the new values ​​to the registers.

For example, the instructions will be:

  • 1RXY → Save register R with value to memory address XY
  • 2RXY → Save register R with value XY
  • BRXY → Go to register R, if xy is it, etc.
  • ARXY → And register R with value in memory address XY

The text file contains something like this in a new line. (in hexadecimal)

  • 120F
  • B007
  • 290b

My problem is copying every single instruction into an unsigned integer ... how to 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]);
}
+3
6

. , :

  • , fopen() return 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
}
+5

strtol(). 16, int/long.

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

. : , atoi() scanf(), . atoi - , , strtol(). scanf(), , - , , scanf(). , short to scanf, .... .

+5

hex, hex.

, %x %x.

+2

( ).

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

'&' , scanf , , . -Wall - .

+2

, ( 4 ) 4 ? , :

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

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

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

If the convert_xdigit_to_int () function simply converts '0' (character) to 0 (int), '1' to 1, '2' to 2, ... '9' to 9, 'a' or "A" is 10, ...

Of course, the pseudo-code is inside the loop, which is executed until the file ends or the array is full. Maybe for fgets () as a condition for a while (...)

while(/*there is space in the array && */ fgets(...)) {
}
0
source

Source: https://habr.com/ru/post/1717197/


All Articles