Thread: Simple hex to ascii char conversion

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Simple hex to ascii char conversion

    Hello, I've written a program that takes a file of hex values, and converts it into characters. I'll end up using(running linux)./byte2char < hexvalues.txt > newvalues.txt to do the conversion. The thing is, it works perfectly, it's just the loop won't end. I run it, and I have to stop it manually with ctrl+c. After only about half a second of running, I'll end up with the converted text and then about 2 million extra lines of nothing. Any help please?

    Thanks

    Code:
    #include<stdio.h>
    
    int main()
    {
        char c;
        char x;
    
    
        while (c!= EOF)
            {
            scanf("%x",&c);
    
            printf("%c",c);
            }
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    scanf RETURNS EOF, it never sets any of its arguments to NULL. There's a difference.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by GReaper View Post
    scanf RETURNS EOF, it never sets any of its arguments to NULL. There's a difference.
    I'm not quite sure what you mean. Would you elaborate a little please?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There are several things wrong with your program. First your format specifier in your scanf() call is for an unsigned int not a char. Second I recommend you use getchar() instead of scanf() if you want a value you will be able to compare against EOF.

    Jim

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Why are you not using argc/argv[] and the fopen functions?

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Thank you jim. I got it to work. At first I tried using just getchar, but it didn't work because getchar grabs letter by letter, and I need to grab them by pairs, because they're hex values separated by spaces. But then with with scanf it ran into trouble too. Apparently if you use c=getchar(), and then scanf after that, it works just fine.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also keep in mind:
    - "getchar()" returns an integer value
    - EOF is an integer, not a character

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII checking and UNICODE conversion
    By TheComedian in forum C Programming
    Replies: 4
    Last Post: 03-22-2011, 04:51 PM
  2. Binary to Ascii Conversion
    By codemaster12 in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 10:57 AM
  3. Simple int -> char conversion failing...
    By michael- in forum C Programming
    Replies: 1
    Last Post: 03-14-2006, 09:04 PM
  4. binary to ASCII conversion
    By loaded_tk in forum C Programming
    Replies: 3
    Last Post: 03-01-2006, 09:28 AM
  5. Hex to Ascii conversion
    By Ryno in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 09:16 AM