How do I assign a hex number in a text file to an int

This is a discussion on How do I assign a hex number in a text file to an int within the C Programming forums, part of the General Programming Boards category; variable. Lets say this is the content of a text file. ///////////////////*******content 1A 1C 1F //////////////////////////end of file How do ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    26

    How do I assign a hex number in a text file to an int

    variable.

    Lets say this is the content of a text file.
    ///////////////////*******content
    1A
    1C
    1F
    //////////////////////////end of file

    How do I assign it to an array of ints.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Here's a big hint:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char str[] = "0A";
        int n = 0;
    
        sscanf(str, "%x", &n);
    
        printf ("%d\n", n);
    
        return 0;
    }//main
    gg
    Last edited by Codeplug; 05-12-2004 at 11:02 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to use an array? Do you know how to open a file? Do you know how to read from a file? These are all things you need to know. For starters, consider reading up on fopen, fclose, fgetc, fgets, fscanf.

    As per the standards of the forum, post your attempt, and we'll offer assistance when you're stuck. Be sure to note any errors, or what specificly you're having trouble doing. But then you already know that, because you surely have read the Announcement regarding it.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    26
    Yes, my question looks like a homework question. I actually did not need help anymore. I got what I needed from a reference site after I posted the first one. fscanf and %x was what I needed.

  5. #5
    Intmainer
    Join Date
    May 2004
    Posts
    7
    hmmm, I knew about fscanf..but what is sscanf...

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

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