^@与控制字符

在应用服务日志中看到了服务器日志记录错误NumberFormatExeception input string ^@^@^@^@,对于 ^@ 这个控制字符没有什么映像了于是vi打开一个临时文件 ctrl + V, ctrl + @ 输入到文件中然后hexdump 看了一下二进制内容发现是

0000000 0a00
0000002

 

本来输入一个字符变成两个了, (不过当时错误判断这个值就是0xa了换行符) 。vi编辑的时候会自动向文件中插入LF。可以输入其他字符比如a,结果是下面这样。

0000000 0a61
0000002

 

下面程序打印了ascii码, 1-31,127是控制字符

#include <stdio.h>

void print_control_char(char c) {
    // 打印控制字符的可见形式
    if (c >= 0 && c <= 31) {
        printf("^%c", c + 64);  // 显示为 ^@, ^A, ..., ^Z 等
    } else if (c == 127) {
        printf("^?");           // DEL字符显示为 ^?
    } else {
        printf(" ");            // 非控制字符为空
    }
}

int main() {
    printf("+-----+--------+--------+--------------+\n");
    printf("| Dec |  Hex   | Char   | Visible Form |\n");
    printf("+-----+--------+--------+--------------+\n");

    // 打印所有控制字符 (0-31, 127)
    for (int i = 0; i <= 127; i++) {
        if (i <= 31 || i == 127) {
            // 构造可见形式的字符串
            char visibleForm[4] = "   ";  // 初始化为空字符串
            if (i <= 31) {
                visibleForm[0] = '^';
                visibleForm[1] = i + 64;  // 转换为可见形式(如 ^A)
            } else if (i == 127) {
                visibleForm[0] = '^';
                visibleForm[1] = '?';    // DEL字符的可见形式为 ^?
            }

            // 打印表格行
            printf("| %3d | 0x%02X   |   %c    | %-12s |\n",
                   i, i, (i == 127 ? '?' : ' '), visibleForm);
            printf("+-----+--------+--------+--------------+\n");
        }
    }

    return 0;
}

 

输出结果

+-----+--------+--------+--------------+
| Dec |  Hex   | Char   | Visible Form |
+-----+--------+--------+--------------+
|   0 | 0x00   |        | ^@           |
+-----+--------+--------+--------------+
|   1 | 0x01   |        | ^A           |
+-----+--------+--------+--------------+
|   2 | 0x02   |        | ^B           |
+-----+--------+--------+--------------+
|   3 | 0x03   |        | ^C           |
+-----+--------+--------+--------------+
|   4 | 0x04   |        | ^D           |
+-----+--------+--------+--------------+
|   5 | 0x05   |        | ^E           |
+-----+--------+--------+--------------+
|   6 | 0x06   |        | ^F           |
+-----+--------+--------+--------------+
|   7 | 0x07   |        | ^G           |
+-----+--------+--------+--------------+
|   8 | 0x08   |        | ^H           |
+-----+--------+--------+--------------+
|   9 | 0x09   |        | ^I           |
+-----+--------+--------+--------------+
|  10 | 0x0A   |        | ^J           |
+-----+--------+--------+--------------+
|  11 | 0x0B   |        | ^K           |
+-----+--------+--------+--------------+
|  12 | 0x0C   |        | ^L           |
+-----+--------+--------+--------------+
|  13 | 0x0D   |        | ^M           |
+-----+--------+--------+--------------+
|  14 | 0x0E   |        | ^N           |
+-----+--------+--------+--------------+
|  15 | 0x0F   |        | ^O           |
+-----+--------+--------+--------------+
|  16 | 0x10   |        | ^P           |
+-----+--------+--------+--------------+
|  17 | 0x11   |        | ^Q           |
+-----+--------+--------+--------------+
|  18 | 0x12   |        | ^R           |
+-----+--------+--------+--------------+
|  19 | 0x13   |        | ^S           |
+-----+--------+--------+--------------+
|  20 | 0x14   |        | ^T           |
+-----+--------+--------+--------------+
|  21 | 0x15   |        | ^U           |
+-----+--------+--------+--------------+
|  22 | 0x16   |        | ^V           |
+-----+--------+--------+--------------+
|  23 | 0x17   |        | ^W           |
+-----+--------+--------+--------------+
|  24 | 0x18   |        | ^X           |
+-----+--------+--------+--------------+
|  25 | 0x19   |        | ^Y           |
+-----+--------+--------+--------------+
|  26 | 0x1A   |        | ^Z           |
+-----+--------+--------+--------------+
|  27 | 0x1B   |        | ^[           |
+-----+--------+--------+--------------+
|  28 | 0x1C   |        | ^\           |
+-----+--------+--------+--------------+
|  29 | 0x1D   |        | ^]           |
+-----+--------+--------+--------------+
|  30 | 0x1E   |        | ^^           |
+-----+--------+--------+--------------+
|  31 | 0x1F   |        | ^_           |
+-----+--------+--------+--------------+
| 127 | 0x7F   |   ?    | ^?           |
+-----+--------+--------+--------------+

 

除了ascii码中的控制字符,类似的还有

C1控制字符

C1控制字符的范围是128–159(十六进制80–9F),主要用于显示器和打印机。

ISO 8859独特字符

不间断空格(NBSP)和软连字符(SHY), 在属性上类似于控制字符,但在ISO 8859中并未真正被定义为控制字符

 

参考及引用

https://www.geeksforgeeks.org/control-characters/

图片from林如

 

Comments are closed.