10

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

2
  • I tried to print them like in html code . console.log("&178;"); Commented Sep 11, 2015 at 12:10
  • And I cant find method to do this Commented Sep 11, 2015 at 12:11

3 Answers 3

12

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...
Sign up to request clarification or add additional context in comments.

1 Comment

OP specifically asked about ASCII characters. 178 + 219 are both printable ASCII characters and they work without any problems at all in Windows. UTF-8 / UTF-16 is of course a whole different problem.
2

Actually, if you only care about ASCII that should not be a real problem at all. You only have to properly escape them. A good reference for this is https://mathiasbynens.be/notes/javascript-escapes

console.log('\xB2 \xDB')

Works for me with recentish node under Windows (cmd shell) and mac OS. For ASCII characters you can just convert them to hex and prepend them with \x in your strings. Give it a try with node -e "console.log('\xB2')"

Comments

0

And when you try this answer, and it works, you might want to try:

node -e "console.log('\x07')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.