节点 JS 控制台日志 ascii 符号

node JS console log ascii symbol

你好
我正在为我的应用程序使用节点 JS,我想在终端中打印 ascii 符号。
Here 是一个 table for ascii symbols .请检查扩展 ASCII 代码字段。我想打印正方形或圆形,例如178或219。


谁能告诉我,怎么做到的?
谢谢

Like several other languages, Javascript suffers from The UTF‐16 Curse. Except that Javascript has an even worse form of it, The UCS‐2 Curse. Things like charCodeAt and fromCharCode only ever deal with 16‐bit quantities, not with real, 21‐bit Unicode code points. Therefore, if you want to print out something like , U+1D49C, MATHEMATICAL SCRIPT CAPITAL A, you have to specify not one character but two “char units”: "\uD835\uDC9C".

请参考这个link:https://dheeb.files.wordpress.com/2011/07/gbu.pdf

您需要的字符不是可打印的 ASCII 字符。在 linux 上,您可以通过 运行 此命令打印所有可打印的 ascii 字符:

 for((i=32;i<=127;i++)) do printf \$(printf '%03o\t' "$i"); done;printf "\n"

 man ascii

所以你可以做的是打印unicode字符。这是所有可用 unicode 字符的列表,您可以 select 一个看起来与您想要的字符几乎相同的字符。

http://unicode-table.com/en/#2764

我已经在 windows 终端上进行了测试,但它仍然没有显示所需的字符,但它正在 linux 上运行。如果它仍然不起作用,您必须确保在 /etc/rc.conf 中设置 LANGUAGE="en_US.UTF-8" 并在 /etc/locale.conf 中设置 LANG="en_US.UTF-8"

所以在节点控制台上打印出这样的东西:

console.log('\u2592 start typing...');

将输出这个结果:

▒ start typing...

实际上,如果您只关心 ASCII,那根本就不是真正的问题。您 必须正确转义 它们。一个很好的参考是 https://mathiasbynens.be/notes/javascript-escapes

console.log('\xB2 \xDB')

适用于 Windows (cmd shell) 和 mac OS 下的最新节点。对于 ASCII 字符,您可以将它们转换为十六进制并在字符串中添加 \x 前缀。试试 node -e "console.log('\xB2')"

当您尝试这个答案并且有效时,您可能想尝试:

节点-e "console.log('\x07')"