Thread: Char to Hex

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Char to Hex

    I'm trying to write a program that takes input from the user (thats a char) and outputs it to the
    monitor in hex form.The program is meant to continuously take input from the user then output to the monitor in hex form until an EOF is detected this triggers the program to close.The following code does this except that I get a lower case 'a' at the end of each output.I think the 'a' has to do with the enter key and if that is the case how can i tell the program to ignore this input from the user. I am noob at programming please help me or at least point me in the right direction.
    Thank you.
    example:

    input from user: ABC
    output to monitor: 41 42 43 a

    Code:
    
    
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char myChar;
        
        while(EOF != (myChar = getchar()))
        {
                  printf("%x ",myChar);
                      
        }
        
      system("pause");
      return 0;
    
    
    }




  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Yes, you are correct that pressing ENTER is seen by getchar, this character is called the newline character and is written as '\n' in C. You could use an if statement to ignore this character from your printout. For example,

    Code:
    while(EOF != (myChar = getchar()))
    {
        /* don't convert newline into hex */
        if (myChar == '\n')
            continue;
    
        printf("%x ",myChar);
    }

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Thank you so much I could kiss you. I have been on this bug for a couple of hours.
    One question though does the continue in the code tell the program that if it sees \n
    then to just ignore it and move on?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by orion314 View Post
    One question though does the continue in the code tell the program that if it sees \n
    then to just ignore it and move on?
    That's right. continue says to go to the next loop iteration. Another variation of this would be the following which does not use continue. However notice that this version will stop the loop as soon as a newline is encountered. That may be what you want, or maybe not.

    Code:
    while((myChar = getchar()) != EOF && myChar != '\n')
    {
        printf("%x ",myChar);
    }
    printf("\n");

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Personally, I suggest avoiding the use of continue, but rather let control fall to the end of the loop body and loop back up again, e.g.,
    Code:
    while (EOF != (myChar = getchar()))
    {
        /* convert into hex, except for newlines */
        if (myChar != '\n')
        {
            printf("%x ", myChar);
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-09-2013, 07:06 PM
  2. Replies: 2
    Last Post: 12-02-2012, 05:25 AM
  3. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  4. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

Tags for this Thread