bash 不显示 126 以上的扩展 ASCII 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8290798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash not displaying extended ASCII characters above 126
提问by KawaiKx
I wrote a small c program to print an extended ASCII char corresponding to value 129
我写了一个小c程序来打印一个对应值129的扩展ASCII字符
#include<stdio.h>
int main(void)
{
char a = 129;
printf("%c\n",a);
return(0);
}
compiled and run it on my unix machine (fedora 16) on bash terminal. it gives wrong display. it shows a question mark in back with white oval background.
在我的 bash 终端上的 unix 机器(fedora 16)上编译并运行它。它给出了错误的显示。它在白色椭圆背景的背面显示一个问号。
infact, if I put a = anything above 126, it is showing same question mark.
事实上,如果我把 a = 放在 126 以上的任何东西,它会显示相同的问号。
why so and how to rectify it?
为什么会这样以及如何纠正它?
回答by jwodder
First of all, I doubt this is bash's fault; bash just finds your program and runs it, while actually displaying your program's output is the job of your terminal application. Secondly, there are no ASCII characters above 127. Trying to print a character in the range 128..255 will emit a byte with the given value, but how that byte gets displayed is determined by your terminal and how it's configured. Most likely, your terminal expects all program output to be encoded in UTF-8; for backwards compatibility with ASCII, all bytes less than 128 are valid characters in UTF-8, but when bytes 128 and above are involved, only certain sequences are valid, and a lone byte with its high bit set is an error. Try printing the bytes 226, 152, and 131, in that order, from a single program; you'll know that your terminal is using UTF-8 if you see a snowman.
首先,我怀疑这是 bash 的错;bash 只是找到你的程序并运行它,而实际显示你的程序的输出是你的终端应用程序的工作。其次,没有超过 127 的 ASCII 字符。尝试打印 128..255 范围内的字符将发出一个具有给定值的字节,但该字节的显示方式取决于您的终端及其配置方式。最有可能的是,您的终端希望所有程序输出都以 UTF-8 编码;为了向后兼容 ASCII,所有小于 128 的字节都是 UTF-8 中的有效字符,但是当涉及 128 及以上的字节时,只有某些序列是有效的,并且设置了高位的单独字节是错误的。尝试从单个程序中按顺序打印字节 226、152 和 131;你'
回答by Grady Player
why do you think that there are ascii chars above 127?
为什么你认为127以上有ascii字符?
http://en.wikipedia.org/wiki/ASCII
http://en.wikipedia.org/wiki/ASCII
ascii is basically 7 bit, 0-127
ascii 基本上是 7 位,0-127
回答by Matt Thomas
I wrote a bash script to display ascii 0-127+ extended 128-255.
It is dependent on the system code page, terminal/console, etc.
我写了一个 bash 脚本来显示 ascii 0-127+ extended 128-255。它取决于系统代码页、终端/控制台等。
Chars 0 - 31, 127 display codes from a table for the non-printables. All others are raw characters printed to screen.
字符 0 - 31、127 显示表格中不可打印的代码。所有其他都是打印到屏幕上的原始字符。
#!/bin/sh
#
# asciie
#
# print the extended ascii table per the codepage of the current computer
#
# cls
echo -e \033c
# headings for each column
echo -n "DEC HEX CHR"
printf " | DEC HEX CHR%.0s" {1..7}
printf "\n"
# div
echo -n "- - - - - - "
printf "| - - - - - - %.0s" {1..7}
printf "\n"
# begin character table for ascii 0 to 127 characters
tb=( 'NUL' 'SOH' 'STX' 'ETX' 'EOT' 'ENQ' 'ACK' 'BEL' 'BS ' 'HT ' 'LF ' 'VT ' 'FF ' 'CR ' 'SO ' 'SI ' \
'DLE' 'DC1' 'DC2' 'DC3' 'DC4' 'NAK' 'SYN' 'ETB' 'CAN' 'EM ' 'SUB' 'ESC' 'FS ' 'GS ' 'RS ' 'US ' \
' ' '! ' '" ' '# ' '$ ' '% ' '& ' \' '( ' ') ' ' ' '+ ' ', ' '- ' '. ' '/ ' \
'0 ' '1 ' '2 ' '3 ' '4 ' '5 ' '6 ' '7 ' '8 ' '9 ' ': ' '; ' '< ' '= ' '> ' '? ' \
'@ ' 'A ' 'B ' 'C ' 'D ' 'E ' 'F ' 'G ' 'H ' 'I ' 'J ' 'K ' 'L ' 'M ' 'N ' 'O ' \
'P ' 'Q ' 'R ' 'S ' 'T ' 'U ' 'V ' 'W ' 'X ' 'Y ' 'Z ' '[ ' '\ ' '] ' '^ ' '_ ' \
'` ' 'a ' 'b ' 'c ' 'd ' 'e ' 'f ' 'g ' 'h ' 'i ' 'j ' 'k ' 'l ' 'm ' 'n ' 'o ' \
'p ' 'q ' 'r ' 's ' 't ' 'u ' 'v ' 'w ' 'x ' 'y ' 'z ' '{ ' '| ' '} ' '~ ' 'DEL' \
)
# end of character table
# display the regular ascii characters 0 to 127
for a in `seq 0 15`
do
let "b=$a+16"; let "c=b+16"; let "d=c+16"; let "e=d+16"; let "f=e+16"; let "g=f+16"; let "h=g+16"
printf "%03d %03X % 3s | %03d %03X % 3s | " $a $a ${tb[$a]} $b $b ${tb[$b]}
printf "%03d %03X \$(printf %03o $c) | %03d %03X \$(printf %03o $d) | " $c $c $d $d
printf "%03d %03X \$(printf %03o $e) | %03d %03X \$(printf %03o $f) | " $e $e $f $f
printf "%03d %03X \$(printf %03o $g) | " $g $g
if [[ $a -lt 15 ]];then
printf "%03d %03X \$(printf %03o $h) \n" $h $h
else
printf "%03d %03X %03s \n" $h $h ${tb[$h]}
fi
done
# end reg ascii
# begin extended ascii section
echo ' '
# display the extended ascii characters 128 to 255
for a in `seq 128 143`
do
let "b=$a+16"; let "c=b+16"; let "d=c+16"; let "e=d+16"; let "f=e+16"; let "g=f+16"; let "h=g+16"
printf "%03d %03X \$(printf %03o $a) | %03d %03X \$(printf %03o $b) | " $a $a $b $b
printf "%03d %03X \$(printf %03o $c) | %03d %03X \$(printf %03o $d) | " $c $c $d $d
printf "%03d %03X \$(printf %03o $e) | %03d %03X \$(printf %03o $f) | " $e $e $f $f
printf "%03d %03X \$(printf %03o $g) | %03d %03X \$(printf %03o $h) \n" $g $g $h $h
done
# div
echo -n "- - - - - - "
printf "| - - - - - - %.0s" {1..7}
printf "\n"
# display footers for each column
echo -n "DEC HEX CHR"
printf " | DEC HEX CHR%.0s" {1..7}
printf "\n"
# EOF #
#!/bin/sh
#
# asciie
#
# print the extended ascii table per the codepage of the current computer
#
# cls
echo -e \033c
# headings for each column
echo -n "DEC HEX CHR"
printf " | DEC HEX CHR%.0s" {1..7}
printf "\n"
# div
echo -n "- - - - - - "
printf "| - - - - - - %.0s" {1..7}
printf "\n"
# begin character table for ascii 0 to 127 characters
tb=( 'NUL' 'SOH' 'STX' 'ETX' 'EOT' 'ENQ' 'ACK' 'BEL' 'BS ' 'HT ' 'LF ' 'VT ' 'FF ' 'CR ' 'SO ' 'SI ' \
'DLE' 'DC1' 'DC2' 'DC3' 'DC4' 'NAK' 'SYN' 'ETB' 'CAN' 'EM ' 'SUB' 'ESC' 'FS ' 'GS ' 'RS ' 'US ' \
' ' '! ' '" ' '# ' '$ ' '% ' '& ' \' '( ' ') ' ' ' '+ ' ', ' '- ' '. ' '/ ' \
'0 ' '1 ' '2 ' '3 ' '4 ' '5 ' '6 ' '7 ' '8 ' '9 ' ': ' '; ' '< ' '= ' '> ' '? ' \
'@ ' 'A ' 'B ' 'C ' 'D ' 'E ' 'F ' 'G ' 'H ' 'I ' 'J ' 'K ' 'L ' 'M ' 'N ' 'O ' \
'P ' 'Q ' 'R ' 'S ' 'T ' 'U ' 'V ' 'W ' 'X ' 'Y ' 'Z ' '[ ' '\ ' '] ' '^ ' '_ ' \
'` ' 'a ' 'b ' 'c ' 'd ' 'e ' 'f ' 'g ' 'h ' 'i ' 'j ' 'k ' 'l ' 'm ' 'n ' 'o ' \
'p ' 'q ' 'r ' 's ' 't ' 'u ' 'v ' 'w ' 'x ' 'y ' 'z ' '{ ' '| ' '} ' '~ ' 'DEL' \
)
# end of character table
# display the regular ascii characters 0 to 127
for a in `seq 0 15`
do
let "b=$a+16"; let "c=b+16"; let "d=c+16"; let "e=d+16"; let "f=e+16"; let "g=f+16"; let "h=g+16"
printf "%03d %03X % 3s | %03d %03X % 3s | " $a $a ${tb[$a]} $b $b ${tb[$b]}
printf "%03d %03X \$(printf %03o $c) | %03d %03X \$(printf %03o $d) | " $c $c $d $d
printf "%03d %03X \$(printf %03o $e) | %03d %03X \$(printf %03o $f) | " $e $e $f $f
printf "%03d %03X \$(printf %03o $g) | " $g $g
if [[ $a -lt 15 ]];then
printf "%03d %03X \$(printf %03o $h) \n" $h $h
else
printf "%03d %03X %03s \n" $h $h ${tb[$h]}
fi
done
# end reg ascii
# begin extended ascii section
echo ' '
# display the extended ascii characters 128 to 255
for a in `seq 128 143`
do
let "b=$a+16"; let "c=b+16"; let "d=c+16"; let "e=d+16"; let "f=e+16"; let "g=f+16"; let "h=g+16"
printf "%03d %03X \$(printf %03o $a) | %03d %03X \$(printf %03o $b) | " $a $a $b $b
printf "%03d %03X \$(printf %03o $c) | %03d %03X \$(printf %03o $d) | " $c $c $d $d
printf "%03d %03X \$(printf %03o $e) | %03d %03X \$(printf %03o $f) | " $e $e $f $f
printf "%03d %03X \$(printf %03o $g) | %03d %03X \$(printf %03o $h) \n" $g $g $h $h
done
# div
echo -n "- - - - - - "
printf "| - - - - - - %.0s" {1..7}
printf "\n"
# display footers for each column
echo -n "DEC HEX CHR"
printf " | DEC HEX CHR%.0s" {1..7}
printf "\n"
# EOF #

