Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3760086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:25:02+00:00 2026-05-19T10:25:02+00:00

I have written a program that works well in C that converts non-readable ASCII

  • 0

I have written a program that works well in C that converts non-readable ASCII to their character values. I would appreciate if a C master? would show me a better way of doing it that I have currently done, mainly this section:

if (isascii(ch)) {
    switch (ch) {
        case 0:
            printControl("NUL");
            break;
        case 1:
            printControl("SOH");
            break;

        .. etc (32 in total)

        case default:
            putchar(ch);
            break;
    }
}

Is it normal to make a switch that big? Or should I be using some other method (input from an ascii table?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:25:03+00:00Added an answer on May 19, 2026 at 10:25 am

    Too many years ago when assembly languages for 8-bit micros were how I spent my time, I would have written something like

    printf("%3.3s", 
           ("NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI "
            "DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ")[3*ch]);
    

    but not because its particularly better. And the multiply by three is annoying because 8-bit micros don’t multiply so it would have required both a shift and an add, as well as a spare register.

    A much more C-like result would be to use a table with four bytes per control, with the NUL bytes included. That allows each entry to be referred to as a string constant, but saves the extra storage for 32 pointers.

    const char *charname(int ch) {
        if (ch >= 0 && ch <= 0x20)
            return ("NUL\0"  "SOH\0"  "STX\0"  "ETX\0"  /* 00..03 */
                    "EOT\0"  "ENQ\0"  "ACK\0"  "BEL\0"  /* 04..07 */
                    "BS\0\0" "HT\0\0" "LF\0\0" "VT\0\0" /* 08..0B */
                    "FF\0\0" "CR\0\0" "SO\0\0" "SI\0\0" /* 0C..0F */
                    "DLE\0"  "DC1\0"  "DC2\0"  "DC3\0"  /* 10..13 */
                    "DC4\0"  "NAK\0"  "SYN\0"  "ETB\0"  /* 14..17 */
                    "CAN\0"  "EM\0\0" "SUB\0"  "ESC\0"  /* 18..1B */
                    "FS\0\0" "GS\0\0" "RS\0\0" "US\0\0" /* 1C..1F */
                    "SP\0\0") + (ch<<2);                /* 20 */
        if (ch == 0x7f)
            return "DEL";
        if (ch == EOF)
            return "EOF";
        return NULL;
    }
    

    I’ve tried to format the main table so its organization is clear. The function returns NULL for characters that name themselves, or are not 7-bit ASCII. Otherwise, it returns a pointer to a NUL-terminated ASCII string containing the conventional abbreviation of that control character, or “EOF” for the non-character EOF returned by C standard IO routines on end of file.

    Note the effort taken to pad each character name slot to exactly four bytes. This is a case where building this table with a scripting language or a separate program would be a good idea. In that case, the simple answer is to build a 129-entry table (or 257-entry) containing the names of all 7-bit ASCII (or 8-bit extended in your preferred code page) characters with an extra slot for EOF.

    See the sources to the functions declared in <ctype.h> for a sample of handling the extra space for EOF.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a java program that is actually works as a gui to
I have written a program that uses qhttp to get a webpage. This works
Well, i have written a simple python program that parses HTML with HTMLParser. Here
I've written a program that works perfectly well when installed on my development PC,
I have written a C++ program (which executes from the command line), that works
I've written a simple application that works with database. My program have a table
I have written a simple program using parallel python, and all works well. However,
I have written a program (using Borland C++ builder) that works fine with normal
I have a program written in C# (Visual Studio), that works on a tray.
I have written a program that will etablish a network connection with a remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.