Use PHP functions in JavaScript

JavaScript ord

Returns the codepoint value of a character

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
function ord (string) {
    // Returns the codepoint value of a character  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/ord    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: ord('K');
    // *     returns 1: 75    // *     example 2: ord('\uD800\uDC00'); // surrogate pair to create a single Unicode character
    // *     returns 2: 65536
    var str = string + '';
    
    var code = str.charCodeAt(0);    if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
        var hi = code;
        if (str.length === 1) {
            return code; // This is just a high surrogate with no following low surrogate, so we return its value;
                                    // we could also throw an error as it is not a complete character, but someone may want to know        }
        var low = str.charCodeAt(1);
        if (!low) {
            
        }        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
    }
    if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
        return code; // This is just a low surrogate with no preceding high surrogate, so we return its value;
                                // we could also throw an error as it is not a complete character, but someone may want to know    }
    return code;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
ord('K');

Should return

1
75

» Example 2

Running

1
ord('\uD800\uDC00'); // surrogate pair to create a single Unicode character

Should return

1
65536

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 ord goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Brett Zamir
14 May '09 Permalink

q  @Ole, three bits of good news here...

One, PHP6 is scheduled to have these work with Unicode: http://lxr.php.net/source/php-src/ext/standard/string.c#3641 and http://lxr.php.net/source/php-src/ext/standard/string.c#3621

Two, it'd be pretty strange to use chr()/ord(), expecting both correct behavior for ASCII (which is reasonable) while also expecting some _specific_ non-Unicode behavior for non-ASCII. While PHP's chr/ord functions before version 6 don't work with Unicode, it does work with ASCII for which it was designed, and these values are the same in both systems (though requiring 2 bytes in Unicode UTF-16 as JavaScript uses internally--though this does not change the behavior of fromCharCode/charCodeAt which work with single 2-byte sequences, not one-byte ones). So, the functions (ours and PHP's) will continue to work fine with ASCII. Just don't go using these functions with Unicode in PHP4 or PHP5. But the fact that they don't work in PHP4 and PHP5 is due to a bug which is now corrected in PHP6.

3) I updated chr() and ord() to work with all Unicode characters, including those rarer ones beyond the Basic Multi-lingual Plane (BMP).

Gravatar
Ole Vrijenhoek
13 May '09 Permalink

q  Hey,
I don't if it is the intention to similate this function,
so it does the exact same thing as php's.
But charCodeAt() returns the unicode or ascii and ord() only does the ascii.

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @ ToshaUst: Thanks for your code! However I don't believe that PHP supports a second argument. What are the use cases? Based on those we can choose to stray from our policy, not to extend function with custom ideas.

Gravatar
ToshaUst
31 Jul '08 Permalink

q  [CODE=&quot;Javascript&quot;]
function ord( string , inHex)
{
var code = string.charCodeAt(0);
if(inHex)
{
var ar = ['A','B','C','D','E','F'],f,s;
f = Math.floor(code/16);
if(f &gt; 9) f = ar[f-10];
s = code % 16;
if(s &gt; 9) s = ar[s-10];
code = f+s;
}
return code;
}
[/CODE]


Contribute a New function