node JS console log ascii symbol

Gor picture Gor · Sep 11, 2015 · Viewed 16k times · Source

Hi
I am using node JS for my app, and I want to print ascii symbols in terminal.
Here is a table for ascii symbols. Please check Extended ASCII Codes field. I want to print square or circle, for example 178 or 219.


Can anyone say me, how can do it?
Thank you

Answer

Endre Simo picture Endre Simo · Sep 11, 2015

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".

Please refer to this link: https://dheeb.files.wordpress.com/2011/07/gbu.pdf

Your desired character is not a printable ASCII character. On linux you can print all the printable ascii characters by running this command:

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

or

 man ascii

So what you can do is to print unicode characters. Here is a list of all the available unicode characters, and you can select one which is looking almost identical with your desired character.

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

I've tested on a windows terminal but it is still not showing the desired character, but it's working on linux. If it's still not working you had to make sure to set LANGUAGE="en_US.UTF-8" in /etc/rc.conf and LANG="en_US.UTF-8" in /etc/locale.conf.

So printing out something like this on node console:

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

will output this result:

▒ start typing...