MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaLang DesignEmacsUnicode ♥

JavaScript: Converting Unicode Character to/from Codepoint, and Decimal/Hexadecimal

,

This pages shows you how to use JavaScript to find a character's Unicode number (called codepoint), and how to get a Unicode char from its codepoint. Also, how to convert from/to decimal and hexadecimal.

Get a Character's Unicode Codepoint

Use this: ‹string›.charCodeAt(‹index›). Example:

// get a Unicode char's codepoint
"α".charCodeAt(0) // returns 945

(945 (decimal) is the codepoint for GREEK SMALL LETTER ALPHA α.)

test here: JavaScript Unicode codepoint test

reference https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/charCodeAt

Get the Character from its Codepoint

Use this: String.fromCharCode(‹n1›, ‹n2›, …), where the ‹n› are codepoints in decimal. It returns a string. Example:

String.fromCharCode(945, 946) // returns "αβ"

test: JavaScript test: Unicode codepoint to char

reference https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/fromCharCode

Converting Decimal from/to Hexadecimal

Decimal to Hexadecimal

// convert decimal to hex
(10).toString(16) // returns "a"

test JavaScript test: decimal to hex

Hexadecimal to Decimal

// convert hexadecimal (string) to decimal
parseInt("a", 16); // returns 10
blog comments powered by Disqus