我写了一个小的c程序来打印对应于值129的扩展ASCII字符
#include<stdio.h>
int main(void)
{
char a = 129;
printf("%c\n",a);
return(0);
}
在bash终端上的unix机器(fedora 16)上编译并运行它。 它给出了错误的显示。它在背面显示一个带有白色椭圆形背景的问号。
事实上,如果我把一个= = 126以上的东西,它显示的是同一个问号。
为什么如此以及如何纠正它?
答案 0 :(得分:6)
首先,我怀疑这是bash的错; bash只是找到你的程序并运行它,而实际显示程序的输出是你的终端应用程序的工作。其次,127以上没有ASCII字符。尝试打印128..255范围内的字符将发出一个具有给定值的字节,但该字节的显示方式由终端及其配置方式决定。最有可能的是,您的终端期望所有程序输出都以UTF-8编码;为了向后兼容ASCII,所有小于128的字节都是UTF-8中的有效字符,但是当涉及字节128及以上时,只有某些序列有效,并且设置了高位的单个字节是错误的。尝试从单个程序按顺序打印字节226,152和131;如果你看到一个雪人,你会知道你的终端正在使用UTF-8。
答案 1 :(得分:3)
答案 2 :(得分:1)
我写了一个bash脚本来显示ascii 0-127
+ extended 128-255
。
它取决于系统代码页,终端/控制台等。
Chars 0 - 31,127显示非printables表的代码。 所有其他都是印在屏幕上的原始字符。
#!/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 #