Linux 中的扩展 Ascii

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5584806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:34:20  来源:igfitidea点击:

Extended Ascii in Linux

linuxextended-ascii

提问by Benjamin

How would I print these characters in Linux?

我如何在 Linux 中打印这些字符?

│ (ascii 179)

│ (ASCII 179)

├ (ascii 195)

├ (ASCII 195)

└ (ascii 192)

└ (ASCII 192)

─ (ascii 196)

─ (ASCII 196)

I cannot find any octal values that would work with echo -e "\0xxx", any ideas?

我找不到任何可以与 echo -e "\0xxx" 一起使用的八进制值,有什么想法吗?

采纳答案by drysdam

After much poring over man printfand info printf, I think I've gotten this to work.

经过大量研究man printfinfo printf,我想我已经开始工作了。

The basic issue seems to be that bash has a built-in printfthat doesn't work. And, despite what the man/info pages, say, \Udoesn't work. \ustill does, though.

基本问题似乎是 bash 有一个printf不起作用的内置函数。而且,尽管 man/info 页面说,\U不起作用。\u不过,仍然如此。

env printf '\u2502'

env printf '\u2502'

gets me a vertical box character.

给我一个垂直的盒子字符。

回答by Ignacio Vazquez-Abrams

Either switch the font to one that is in PC-8/CP437 encoding, or use the Unicode values for those characters instead, encoded into the current charset.

要么将字体切换为 PC-8/CP437 编码的字体,要么使用这些字符的 Unicode 值,编码为当前字符集。

回答by AnxiousNut

You can use the exact same codes you provided or of the extended ASCII character set (e.g. 195 for ├) if you've got the right encoder to display the characters.

如果您有正确的编码器来显示字符,您可以使用您提供的完全相同的代码或扩展的 ASCII 字符集(例如 195 表示 ├)。

On Linux, we lack the non-standard extended ASCII character set support - which is why it's not displayed. However, I found another character set that's available for Linux and is almostsimilar to the extended ASCII character set. It's IBM855.

在 Linux 上,我们缺乏非标准的扩展 ASCII 字符集支持 - 这就是它不显示的原因。但是,我发现了另一个可用于 Linux 的字符集,它几乎类似于扩展的 ASCII 字符集。是IBM855。

All you have to do is changed the character encoding of your command line application to IBM855. All popular box drawing characters have the same code of the extended ASCII character set - which is the most important.

您所要做的就是将命令行应用程序的字符编码更改为 IBM855。所有流行的方框图字符都具有与扩展 ASCII 字符集相同的代码——这是最重要的。

You may compare the sets by this imageand this image.

您可以通过此图像和此图像比较集合。

PS: If you're using gnome-terminal, you can add IBM855 charset by clicking the "Terminal" menu from the menu bar -> "set character encoding" -> "Add or Remove". Look for IBM855, and add it. Now just choose the encoding from "terminal"->"set character encoding"->"Cyrillic (IBM855)".

PS:如果您使用的是gnome-terminal,您可以通过单击菜单栏中的“终端”菜单->“设置字符编码”->“添加或删除”来添加IBM855字符集。查找 IBM855,并添加它。现在只需从“终端”->“设置字符编码”->“西里尔文(IBM855)”中选择编码。

They boxes were enough for my homework. Hope this helps. :)

他们的盒子足够我做作业了。希望这可以帮助。:)

回答by shmatt

Because some people may still want to know this...

因为有些人可能还想知道这个......

See the lines that uses iconv to translate.

请参阅使用 iconv 进行翻译的行。

To print all ascii/extended ascii codes CP437 in Linux/bash script:

要在 Linux/bash 脚本中打印所有 ascii/extended ascii 代码 CP437:

# heading index with div line
printf "\n      "; # indent

for x in {0..15}; do printf "%-3x" $x; done;
printf "\n%46s\n" | sed 's/ /-/g;s/^/      /';

# two lines with dots to represent control chars
c=$(echo "fa" | xxd -p -r | iconv -f 'CP437//' -t 'UTF-8')
printf "%32s" | sed 's/../'"$c"'  /g;s/^/  0   /;s/$/\n\n/'
printf "%32s" | sed 's/../'"$c"'  /g;s/^/  1   /'

# convert dec to codepage 437 in a table
for x in {32..255};
do

  # newline every 16 translated code values
  (( x % 16 == 0 )) && printf "\n\n"

  # left index numbers
  let "n = x % 15"
  (( (x % 16) == 0 )) && printf "%-4x" $n | sed 's/0/f/;s/^/  /'

  # conversion of x integer value to symbol
  printf "%02x" $x | xxd -p -r | iconv -f 'CP437//' -t 'UTF-8' | sed 's/.*/&  /'

  # div line
  (( x == 127 )) && printf "%46s" | sed 's/ /-/g;s/^/      /;i\ '

done
printf "%46s" | sed 's/ /-/g;s/^/\n      /;s/$/\n      /'; # div line
for x in {0..15}; do printf "%-3x" $x; done;
echo