c++打印ascii码表--cout版

…衆ロ難τιáo~ 提交于 2020-01-29 23:14:19
/*
 * C++ Program to Print ASCII table (0 - 127)
 */
 
#include<iostream>
#include<iomanip>
using namespace std;
 
char const* character[] = {"NULL(空)", "SOH(标题开始)", "STX(正文开始)", "ETX (正文结束)", "EOT (传送结束)", 
                              "ENQ (询问)", "ACK (确认)", 
                            "\\a","\\b","\\t","\\n","\\v","\\f","\\r","SO(移出)","SI(移入)", 
                              "DLE(退出数据链)",
                            "DC1 (设备控制1)", "DC2(设备控制2)", "DC3(设备控制3)", "DC4(设备控制4)",
                              "NAK (反确认)", "SYN (同步空闲)", "ETB (传输块结束)", "CAN (取消)",
                            "EM (媒介结束)", "SUB (替换)", "ESC (退出)", "FS (文件分隔符)",
                              "GS (组分隔符)", "RS (记录分隔符)", "US (单元分隔符)", "(空格)"};
 
int main()
{
    
    char c;
    int row;
    cout << " ASCII Table" << endl << "=============" << endl;
    
    for(int i = 0; i < 32; i++)
    {
        row = i;
        while (row <= 127) {
            if (row <= 32){
               
                cout << setfill('0') << setw(2) << setbase(16)
                     << row << " = " << setw(15) << setfill(' ')
                     << character[row] << " | ";
            }
            else if (row > 32 && row < 127)
            {
                c = row;
                cout << setfill('0') << setw(2) << setbase(16)
                     << row << " = " << setw(15) << setfill(' ')
                     << c << " | ";
            }
            else
                cout << setfill('0') << setw(2) << setbase(16)
                     << row << " = " << setw(15) << setfill(' ')
                     << "DEL" << " | ";
            row = row + 32;
        }
        cout << endl;
    }
} 

 输出:

 ASCII Table =============
01 = SOH(标题开始) | 21 =               ! | 41 =               A | 61 =               a | 
02 = STX(正文开始) | 22 =               " | 42 =               B | 62 =               b | 
03 = ETX (正文结束) | 23 =               # | 43 =               C | 63 =               c | 
04 = EOT (传送结束) | 24 =               $ | 44 =               D | 64 =               d | 
05 =    ENQ (询问) | 25 =               % | 45 =               E | 65 =               e | 
06 =    ACK (确认) | 26 =               & | 46 =               F | 66 =               f | 
07 =              \a | 27 =               ' | 47 =               G | 67 =               g | 
08 =              \b | 28 =               ( | 48 =               H | 68 =               h | 
09 =              \t | 29 =               ) | 49 =               I | 69 =               i | 
0a =              \n | 2a =               * | 4a =               J | 6a =               j | 
0b =              \v | 2b =               + | 4b =               K | 6b =               k | 
0c =              \f | 2c =               , | 4c =               L | 6c =               l | 
0d =              \r | 2d =               - | 4d =               M | 6d =               m | 
0e =      SO(移出) | 2e =               . | 4e =               N | 6e =               n | 
0f =      SI(移入) | 2f =               / | 4f =               O | 6f =               o | 
10 = DLE(退出数据链) | 30 =               0 | 50 =               P | 70 =               p | 
11 = DC1 (设备控制1) | 31 =               1 | 51 =               Q | 71 =               q | 
12 = DC2(设备控制2) | 32 =               2 | 52 =               R | 72 =               r | 
13 = DC3(设备控制3) | 33 =               3 | 53 =               S | 73 =               s | 
14 = DC4(设备控制4) | 34 =               4 | 54 =               T | 74 =               t | 
15 = NAK (反确认) | 35 =               5 | 55 =               U | 75 =               u | 
16 = SYN (同步空闲) | 36 =               6 | 56 =               V | 76 =               v | 
17 = ETB (传输块结束) | 37 =               7 | 57 =               W | 77 =               w | 
18 =    CAN (取消) | 38 =               8 | 58 =               X | 78 =               x | 
19 = EM (媒介结束) | 39 =               9 | 59 =               Y | 79 =               y | 
1a =    SUB (替换) | 3a =               : | 5a =               Z | 7a =               z | 
1b =    ESC (退出) | 3b =               ; | 5b =               [ | 7b =               { | 
1c = FS (文件分隔符) | 3c =               < | 5c =               \ | 7c =               | | 
1d = GS (组分隔符) | 3d =               = | 5d =               ] | 7d =               } | 
1e = RS (记录分隔符) | 3e =               > | 5e =               ^ | 7e =               ~ | 
1f = US (单元分隔符) | 3f =               ? | 5f =               _ | 7f =             DEL | 

如需使表格严格对齐,需要去掉所有中文字符,如下:

/*
 * C++ Program to Print ASCII table (0 - 127)
 */
 
#include<iostream>
#include<iomanip>
using namespace std;
 
char const* character[] = {"NULL", "SOH", "STX", "ETX ", "EOT ", 
                              "ENQ ", "ACK", 
                            "\\a","\\b","\\t","\\n","\\v","\\f","\\r","SO","SI", 
                              "DLE",
                            "DC1 ", "DC2", "DC3", "DC4",
                              "NAK", "SYN", "ETB", "CAN ",
                            "EM ", "SUB", "ESC", "FS ",
                              "GS ", "RS ", "US ", "(space)"};
 
int main()
{
   
    char c;
    int row;
    cout << " ASCII Table" << endl << "=============" << endl;
    
    for(int i = 0; i < 32; i++)
    {
        row = i;
        while (row <= 127) {
            if (row <= 32){
                
                cout << setfill('0') << setw(2) << setbase(16)
                     << row << " = " << setw(15) << setfill(' ')
                     << character[row] << " | ";
            }
            else if (row > 32 && row < 127)
            {
                c = row;
                cout << setfill('0') << setw(2) << setbase(16)
                     << row << " = " << setw(15) << setfill(' ')
                     << c << " | ";
            }
            else
                cout << setfill('0') << setw(2) << setbase(16)
                     << row << " = " << setw(15) << setfill(' ')
                     << "DEL" << " | ";
            row = row + 32;
        }
        cout << endl;
    }
} 

输出:

 ASCII Table
=============
00 =         NULL | 20 =      (space) | 40 =            @ | 60 =            ` | 
01 =          SOH | 21 =            ! | 41 =            A | 61 =            a | 
02 =          STX | 22 =            " | 42 =            B | 62 =            b | 
03 =         ETX  | 23 =            # | 43 =            C | 63 =            c | 
04 =         EOT  | 24 =            $ | 44 =            D | 64 =            d | 
05 =         ENQ  | 25 =            % | 45 =            E | 65 =            e | 
06 =          ACK | 26 =            & | 46 =            F | 66 =            f | 
07 =           \a | 27 =            ' | 47 =            G | 67 =            g | 
08 =           \b | 28 =            ( | 48 =            H | 68 =            h | 
09 =           \t | 29 =            ) | 49 =            I | 69 =            i | 
0a =           \n | 2a =            * | 4a =            J | 6a =            j | 
0b =           \v | 2b =            + | 4b =            K | 6b =            k | 
0c =           \f | 2c =            , | 4c =            L | 6c =            l | 
0d =           \r | 2d =            - | 4d =            M | 6d =            m | 
0e =           SO | 2e =            . | 4e =            N | 6e =            n | 
0f =           SI | 2f =            / | 4f =            O | 6f =            o | 
10 =          DLE | 30 =            0 | 50 =            P | 70 =            p | 
11 =         DC1  | 31 =            1 | 51 =            Q | 71 =            q | 
12 =          DC2 | 32 =            2 | 52 =            R | 72 =            r | 
13 =          DC3 | 33 =            3 | 53 =            S | 73 =            s | 
14 =          DC4 | 34 =            4 | 54 =            T | 74 =            t | 
15 =          NAK | 35 =            5 | 55 =            U | 75 =            u | 
16 =          SYN | 36 =            6 | 56 =            V | 76 =            v | 
17 =          ETB | 37 =            7 | 57 =            W | 77 =            w | 
18 =         CAN  | 38 =            8 | 58 =            X | 78 =            x | 
19 =          EM  | 39 =            9 | 59 =            Y | 79 =            y | 
1a =          SUB | 3a =            : | 5a =            Z | 7a =            z | 
1b =          ESC | 3b =            ; | 5b =            [ | 7b =            { | 
1c =          FS  | 3c =            < | 5c =            \ | 7c =            | | 
1d =          GS  | 3d =            = | 5d =            ] | 7d =            } | 
1e =          RS  | 3e =            > | 5e =            ^ | 7e =            ~ | 
1f =          US  | 3f =            ? | 5f =            _ | 7f =          DEL | 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!