在 Javascript 中打印所有 ASCII 字符

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

Printing all ASCII characters in Javascript

javascriptunicodeascii

提问by Amit Tomar

I need to do something like this:

我需要做这样的事情:

  1. Have a variable of some type.
  2. Run in a loop and assign all the possible ASCII characters to this variable and print them, one by one.
  1. 有某种类型的变量。
  2. 循环运行并将所有可能的 ASCII 字符分配给该变量并一一打印。

Is something similar possible for UNICODE also?

UNICODE 也有类似的可能吗?

回答by Michael Krelin - hacker

I'm not sure how exactly you want to print, but this will console.logprintable ascii

我不确定您要打印的准确程度,但这将是console.log可打印的 ascii

for(var i=32;i<127;++i) console.log(String.fromCharCode(i));

You can document.writethen if that's your intention. And if the environment is unicode, it should work for unicode as well, I believe.

document.write如果这是您的意图,那么您可以。如果环境是 unicode,我相信它也应该适用于 unicode。

回答by Guffa

There are some of the ASCII characters that are non-printable, but for example getting the characters from 32 (space) to 126 (~), you would use:

有一些 ASCII 字符是不可打印的,但例如将字符从 32(空格)到 126(~),您可以使用:

var s = '';
for (var i = 32; i <= 127; i++) s += String.fromCharCode(i);

The unicode character set has more than 110,000 different characters (see Unicode), but a normal font doesn't contain all of them, so you can't display them anyway. You would have to specify what parts of the character space you are interested in.

unicode 字符集有超过 110,000 个不同的字符(请参阅Unicode),但普通字体并不包含所有这些字符,因此您无论如何都无法显示它们。您必须指定您对字符空间的哪些部分感兴趣。

回答by Jukka K. Korpela

Others have shown how to print the printable Ascii characters. It is possible to print all other Ascii characters, too, though they are control characters with system-dependent effect (often no effect). To create a string containing all Ascii characters into a string, you could do this:

其他人已经展示了如何打印可打印的 Ascii 字符。也可以打印所有其他 Ascii 字符,尽管它们是具有系统相关效果(通常没有效果)的控制字符。要将包含所有 Ascii 字符的字符串创建为字符串,您可以执行以下操作:

var s = '';
for (var i = 0; i <= 127; i++) s += String.fromCharCode(i);

Unicode is much more tricky, because the Unicode coding space, from 0 to 0x10FFFF, contains a large number of unassigned code points as well as code points designated as noncharacters. There are also Private Use code points, which may be used to denote characters by “private agreement” but have no generally assigned meaning. Moreover, many Unicode characters are nonspacing, i.e. meant to combine with the preceding character (e.g., turning “a” to “a”), so you can't visually print them in a row. There is no simple way in JavaScript to determine, from a integer, the class of the corresponding code point – you might need to read the UnicodeData.txt file, parse it, and use the information there to classify code points.

Unicode 要复杂得多,因为 Unicode 编码空间(从 0 到 0x10FFFF)包含大量未分配的代码点以及指定为非字符的代码点。还有专用代码点,可用于表示“私人协议”中的字符,但没有一般指定的含义。此外,许多 Unicode 字符是无间距的,即意味着与前面的字符组合(例如,将“a”变为“a”),因此您无法直观地将它们连续打印。在 JavaScript 中没有简单的方法可以根据整数确定相应代码点的类——您可能需要读取 UnicodeData.txt 文件,解析它,并使用那里的信息对代码点进行分类。

Finally, there is the programming issue that the JavaScript concept of character corresponds to a 16-bit code unit (not code point), and any Unicode code point larger than 0xFFFF needs to be represented using two code units (so-called surrogates). If you are using JavaScript in the context of an HTML document and you want to print characters in th HTML content, then the simplest way is to use character references like &#x10400;(which denotes the Unicode character at code point 10400hexadecimal) and assign the string to the innerHTMLproperty of an element.

最后,还有一个编程问题,JavaScript 的字符概念对应一个 16 位代码单元(不是代码点),任何大于 0xFFFF 的 Unicode 代码点都需要使用两个代码单元(所谓的代理)来表示。如果您在 HTML 文档的上下文中使用 JavaScript 并且想要打印 HTML 内容中的字符,那么最简单的方法是使用字符引用&#x10400;(表示10400十六进制代码点处的 Unicode 字符)并将字符串分配给innerHTML元素的属性。

If you need to write ranges of Unicode characters, you might take a look at the Full Unicode Inpututility that I recently wrote. Its source code illustrates some ways of dealing with Unicode characters in JavaScript.

如果您需要编写 Unicode 字符范围,您可以查看我最近编写的Full Unicode Input实用程序。它的源代码说明了在 JavaScript 中处理 Unicode 字符的一些方法。