String.fromCharCode(132) returns empty string

375 views Asked by At

I'm trying to make an encryption system that take the char code of every character in a string then applies a function to them to create new char codes. Then, make string from those char codes. every thing is successful, but I've noticed that when String.fromCharCode is called on 132 it returns an empty string. is there way to fix this?

i've tried:

'\u{132}' // works but '\u{132}.charCodeAt(0)' returns 306
1

There are 1 answers

0
Daniel Beck On

This is because you're working in two different number systems. charCode is in decimal, but \u notation uses hexadecimal. 306 is the decimal representation of the hexadecimal \u{132}:

console.log('\u{132}'.charCodeAt(0)) // returns in decimal
console.log(parseInt(132, 16)) // convert hex to decimal

console.log('\u{132}') // the character represented in hex
console.log(String.fromCharCode(306)) // and represented in decimal

String.fromCharCode(132) actually returns a control code, not an empty string.