Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node JS console log ascii symbol

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

like image 869
Gor Avatar asked Sep 11 '15 11:09

Gor


People also ask

Can we use console log in node JS?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

What is ASCII value of A to Z?

The ASCII value of the lowercase alphabet is from 97 to 122. And, the ASCII value of the uppercase alphabet is from 65 to 90.

What is ASCII value?

The ascii value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127. For example, the ascii value of 'A' is 65.

What is ASCII JavaScript?

ASCII stands for American Standard Code for Information Interchange. ASCII is a numeric value that is given to different characters and symbols for computers to store and manipulate. For example, the ASCII value of the letter 'A' is 65. Resource: ASCII chart of all 127 characters in JavaScript.


2 Answers

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...
like image 76
Endre Simo Avatar answered Sep 21 '22 20:09

Endre Simo


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')"

like image 36
Christian Ulbrich Avatar answered Sep 20 '22 20:09

Christian Ulbrich