JavaScript chr
Converts a codepoint number to a character
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 | function chr (codePt) { // Converts a codepoint number to a character // // version: 1008.1718 // discuss at: http://phpjs.org/functions/chr // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Brett Zamir (http://brett-zamir.me) // * example 1: chr(75); // * returns 1: 'K' // * example 1: chr(65536) === '\uD800\uDC00'; // * returns 1: true if (codePt > 0xFFFF) { // Create a four-byte string (length 2) since this code point is high // enough for the UTF-16 encoding (JavaScript internal use), to // require representation with two surrogates (reserved non-characters // used for building other characters; the first is "high" and the next "low") codePt -= 0x10000; return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF)); } else { return String.fromCharCode(codePt); } } |
Examples
Running
1 2 | chr(75); chr(65536) === '\uD800\uDC00'; |
Should return
1 | true |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have chr goodness in JavaScript.
I'm quite partial to Mediawiki. Well-tested, familiar syntax (at least among wikis), easy to install, extensible, well-supported by the community, etc. See http://www.mediawiki.org/wiki/Download and links on the page for 'how-to' articles such as customizing the logo, etc. (e.g., http://www.mediawiki.org/wiki/Manual:Skins and http://www.mediawiki.org/wiki/Manual:Gallery_of_user_styles )
@ Brett Zamir: Ok i'm definitely cool with that. Just need to figure out what wiki to use and how to nicely integrate it into this site (cakephp). any suggestions are welcome
Nice one, thanks Roland. Used the same approach in my LGPL Firefox Unicode chart view extension - https://addons.mozilla.org/en-US/firefox/addon/5235 (I should make an HTML 5 version of that one if somebody else doesn't, now that SQL can be used client-side to store character descriptions.)
Anyhow, Kevin, what do you think of a public wiki for demos of code like this one? We keep getting asked for demos, so maybe our users would like a little workspace for stuff like this (or derivatives of the functions, etc.). Wouldn't replace the compiler, and central review is still helpful, but we might see others be able to help out a little with introduction, offering special tricks, etc. What do you think?
just a simple way to use this script:
function charmap(font,size) {
if (font==null) font="Arial";
if (size==null) size=24;
document.write("<style>\n"+
"* { font:normal bold "+size+"pt "+font+"; }\n"+
"td { text-align:center; }\n"+
"</style>\n");
document.write("<table>\n");
for (x=0;x<16;x++) {
document.write("<tr>\n");
for (y=0;y<16;y++) {
document.write("<td>"+chr(16*x+y)+"</td>\n");
}
document.write("</tr>\n");
}
document.write("</table>\n");
}
@ fumbling fellow: Yeah I recently moved the css to a separate file, but I messed up apparently. Thank you, fixed it now!
Looks like the code is working, the css file is not found so the cells are not changing color.
http://kevin.vanzonneveld.net/css/pj_tester.css
I found the function I was looking for though.
print_r


Kevin van Zonneveld
8 Jul '09