Reputation: 31048
I have a Hungarian statement that I would like to log to the console like this:
console.log('Probléma a működésben.');
But it prints the following:
> Probléma a működésben.
The non ASCII characters are messed up, but I don't think the reason for this is that the console doesn't support Unicode characters, because if I paste the log straight into the console it produces the proper output.
I tested it in Opera, Firefox, Chrome. Happens in Win 8.1 and OSX too, so we can say this is general.
Are there other things that should persist for the proper Unicode console logging, like HTML charset or file encode?
Upvotes: 17
Views: 26706
Reputation: 699
At first, the source file must be encoded UTF-8.
You have to put chcp 65001
for UTF-8 console encoding.
Explore the character hexcode. The full understanding of unicode character form is there: unicode I am using the \u{HEX} form, it is universal.
Then use hex and template for evaluate the character.
test #1
var chr={hex:"03A9"} //Ω
chr.dec=parseInt("0x"+chr.hex,16);
chr.htmlx=String.fromCharCode(38)+"#x"+chr.hex+";";
chr.htmld=String.fromCharCode(38)+"#"+chr.dec+";";
chr.unicode="`"+String.fromCharCode(92)+"u{"+chr.hex+"}`";
chr.grafema=eval(chr.unicode);
console.log(JSON.stringify(chr,0,1));
console.log("unicode", chr.unicode);
console.log("decimal",String.fromCharCode(chr.dec));
console.log("grafema", chr.grafema);
Result:
{
"hex": "03A9",
"dec": 937,
"htmlx": "Ω",
"htmld": "Ω",
"unicode": "`\\u{03A9}`",
"grafema": "Ω"
}
unicode `\u{03A9}`
decimal Ω
grafema Ω
The String.fromCharCode() works. test #2
var chr={hex:"1F353"} //🍓
chr.dec=parseInt("0x"+chr.hex,16);
chr.htmlx=String.fromCharCode(38)+"#x"+chr.hex+";";
chr.htmld=String.fromCharCode(38)+"#"+chr.dec+";";
chr.unicode="`"+String.fromCharCode(92)+"u{"+chr.hex+"}`";
chr.grafema=eval(chr.unicode);
console.log(JSON.stringify(chr,0,1));
console.log("unicode", chr.unicode);
console.log("decimal",String.fromCharCode(chr.dec));
console.log("grafema", chr.grafema);
{
"hex": "1F353",
"dec": 127827,
"htmlx": "🍓",
"htmld": "🍓",
"unicode": "`\\u{1F353}`",
"grafema": "🍓"
}
unicode `\u{1F353}`
decimal
grafema 🍓
The String.fromCharCode() does not works, due to greater than 0xFFFF has truncated.
Upvotes: 0
Reputation: 11
No, you must use this table UNICODE en internet, for example, with the character: 'Ã' do this:
string.replace(/\u00c3/g, 'é'); //And you can change all the symbols by what you want
This is with the first character you don't want and so so with the other characters
à == u00c3 and UNICODE
Upvotes: 0
Reputation: 7980
First way: you have to find your characters in unicode table
console.log( '\u03A9' )
; // Ω
Second way - use unidecode npm package.
Upvotes: 14
Reputation: 31048
I found out that if you set the proper charset in a <meta>
tag in the <head>
it will work:
<meta charset="UTF-8">
Upvotes: 11
Reputation: 197
You can specify the encoding of your file by setting the charset attribute
<script src="script.js" type="text/javascript" charset="utf-8"/>
Upvotes: 5