Thread: A way to print the control characters?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    99

    A way to print the control characters?

    Is there a way to print the control characters (0 to 31 decimal in ASCII) so that you can actually see the control character on the output screen? I'm talking about the "upwards carrot" and the letter following it. Right now, I get strange objects like smiley faces. How do I get the upward carrot? For example:

    Code:
    int main()
    {
        int ex_ample;
    
        printf("Enter a number to convert to its ASCII character\n");
    
        ex_ample=getch();  //Say I enter "Control-B"
    
        printf("test %c, %d\n", ex_ample ,ex_ample); //I get a smiley face but I would like to see the upwards carrot and "B"
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Those sequences, like "^B" for example, are merely conventions used in some places. I'm not sure the full mapping of ASCII control codes (I'm assuming you're using ASCII or compatible) to the "carrot" sequences is documented anywhere, or if there's even a name for it that you can Google. Regardless, there's no special way to print them out in C, you have to do this by hand (it's easy). Simply check the value of ex_ample, and if it's 1-26, print the appropriate letter after the carrot (A-Z). I'm not sure what you expect to print for nul (I think I've seen ^@) or char values 27-31, you will have to figure that out on your own. As a starter, just a basic chain of if/else if should do it:
    Code:
    if (ex_ample < 0)
        // something wrong here
    else if (ex_ample == 0)
        print nul sequence
    else if (ex_ample <= 26)
        print ^A, ^B, ^C as appropriate
    else if (ex_ample < 32)
        print some other sequence
    else if (ex_ample < 127)
        // normal char
    Note, all of this relies on you using ASCII or an ASCII-compatible character set. This is almost certainly the case, so you shouldn't have much to worry about. Just know that somewhere out there, there are systems that this code won't work right on, because they use other character sets with different control codes.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    I suspected something like this... I was hoping to not have to manually enter all 31 entries though in a giant switch statement. Thanks!

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by gratiafide View Post
    I was hoping to not have to manually enter all 31 entries though in a giant switch statement.
    Well, how about
    Code:
    char *ascii(const int code, char buffer[3])
    {
        if (code >= 0 && code < 32) {
            buffer[0] = '^';
            buffer[1] = '@' + code;
            buffer[2] = '\0';
        } else {
            buffer[0] = code;
            buffer[1] = '\0';
        }
    
        return (char *)buffer;
    }
    The ^c map to ASCII control codes 64 less than character c.

    You can use that as follows:
    Code:
        int a, b, c;
        char astr[3], bstr[3], cstr[3];
    
        printf("a is %s, b is %s, and c is %s.\n", ascii(a, astr), ascii(b, bstr), ascii(c, cstr));
    If you want C string escapes, then
    Code:
    char *ascii(const int code, char buffer[5])
    {
        if (code < 0)
            buffer[0] = '\0';
        else
        if (code < 32) {
            buffer[0] = '\\';
            if (code >= 7 && code <= 13) {
                static const char control[8] = "abtnvfr";
                buffer[1] = control[code - 7];
                buffer[2] = '\0';
            } else {
                buffer[1] = '0';
                buffer[2] = '0' + (code / 8);
                buffer[3] = '0' + (code % 8);
                buffer[4] = '\0';
            }
        } else
        if (code < 127) {
            buffer[0] = code;
            buffer[1] = '\0';
        } else
        if (code < 256) {
            buffer[0] = '\\';
            buffer[1] = '0' + ((code / 64) % 8);
            buffer[2] = '0' + ((code / 8) % 8);
            buffer[3] = '0' + (code % 8);
            buffer[4] = '\0';
        } else
            buffer[0] = '\0';
    
        return (char *)buffer;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to print number of characters per line
    By Slyvester Ping in forum C Programming
    Replies: 25
    Last Post: 03-30-2012, 03:11 AM
  2. I got weird outputs characters when I print
    By aama100 in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2008, 12:13 AM
  3. how to print characters of own language?
    By kantze in forum C Programming
    Replies: 4
    Last Post: 10-17-2006, 12:22 PM
  4. How do you create and print an array of characters
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 07-09-2002, 04:22 PM
  5. Graphics won't print characters
    By cheesehead in forum C++ Programming
    Replies: 0
    Last Post: 11-12-2001, 11:45 AM