Thread: ASCII and EBCDIC

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    EBCDIC is apparently still used in IBM mainframes, although ASCII is "tolerated" in that you can edit ascii files and the cpu has instructions for conversion. EBCDIC - Wikipedia

    I would use int since character literals are ints in C anyway, and functions like getchar return an int.
    Code:
    #include <stdio.h>
    #include <ctype.h>
     
    int get_index(int ch)
    {
        ch = toupper(ch);
        if (ch >= 'A' && ch <= 'I') return ch - 'A';
        if (ch >= 'J' && ch <= 'R') return ch - 'J' + 9;
        if (ch >= 'S' && ch <= 'Z') return ch - 'S' + 18;
        return -1;
    }
     
    int main()
    {
        int ch;
        while ((ch = getchar()) != EOF)
            printf("%d\n", get_index(ch));
        return 0;
    }
    A switch might actually be faster.
    Code:
    int get_index(int ch)
    {
        switch (toupper(ch))
        {
        case 'A': return 0;
        case 'B': return 1;
        }
        return -1;
    }
    Last edited by john.c; 03-24-2022 at 09:59 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to understand format of old FTP connection using EBCDIC
    By technokid in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-14-2011, 09:48 PM
  2. write a variable block EBCDIC file
    By htmanbloodmoney in forum C++ Programming
    Replies: 0
    Last Post: 05-26-2010, 02:18 PM
  3. EBCDIC to ASCII
    By C of Green in forum C# Programming
    Replies: 5
    Last Post: 05-21-2009, 07:07 AM
  4. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  5. EBCDIC files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 03:37 PM

Tags for this Thread