Hex

This is a discussion on Hex within the C Programming forums, part of the General Programming Boards category; how can i read an hex numbers from file?...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    30

    Hex

    how can i read an hex numbers from file?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    ...
    fscanf (fpointer, "%x", &i);
    ?

  3. #3
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    This works for me...

    int amount_of_numbers, number[100]
    FILE *input;
    input = fopen("file.txt", "r");
    if (input==NULL) {
    exit(1);
    }
    for (i=0; i<amount_of_numbers; i++) {
    fscanf(input, "%x", &number[i]);
    }
    fclose(input);

    '%x' tells scanf to read in a hex number rather than an integer.

    the array 'number' should now contain the values in hex in the order they were in the file.

    Hope this works...

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    30
    i tried this method in the first time but for some reason it's not read the same numbers that i saw in the hex editor

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Oooooooh... that's what you want to know.

    Try this...
    Code:
    #include <stdio.h>
    #define NUMS 10
    typedef char byte;
    
    int main (int argc, char * args[])
    {
     FILE * f;
     byte b;
     putc ('\n');
     f = fopen (args[1], "r");
    
     for (i = NUMS; i != 0; i--)
     {
      b = fgetc (f);
      printf ("%3.2X", (int) b);
     }
     putc ('\n');
     return;
    }
    Not really sure if this program will work, but maybe it gets the point across? Let's assume this program is named disphex.exe then...
    disphex storm.cam
    will display the first ten bytes of the file storm.cam in hex format.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    30
    in what your method is difference from the other method

    and for what the %3.2X?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 05:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 06:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21