How to transform the following code from PHP to JavaScript?
$str = '';
for ($i = 0; $i < 256; $i++) {
$str .= chr($i);
}
I know String.fromCharCode(n)
in JavaScript is similar to chr(n)
in PHP, but it seems they return different characters when n is greater than 127.
How to transform the following code from PHP to JavaScript?
$str = '';
for ($i = 0; $i < 256; $i++) {
$str .= chr($i);
}
I know String.fromCharCode(n)
in JavaScript is similar to chr(n)
in PHP, but it seems they return different characters when n is greater than 127.
- What's your expected output? – Starfish Commented May 23, 2016 at 16:25
- You need to include your inputs and outputs, as well as any errors you've run into. – ssube Commented May 23, 2016 at 16:29
- What do you want to do exactly? JavaScript doesn't use ASCII at all and your strings probably don't do it either (we are in 2016!). – Álvaro González Commented May 23, 2016 at 16:33
- Why is this question on hold? OP asks for a js implementation of a given PHP reference code. You can't be more precise than that. – le_m Commented May 25, 2016 at 11:09
1 Answer
Reset to default 7PHP's chr() function interprets input from 0..127 as ascii values and input from 128..255 as IBM code page 437 (CP437, proprietary extended ascii) values. This is the plete ASCII table used by PHP: http://www.asciitable./
JavaScript's String.fromCharCode(i) method interprets input as UTF-8 values, which are similar to ascii (from 0..127) but not the IBM CP437 extended ascii (from 128..255).
You can try to recreate PHP's chr() function in JavaScript, e. g. as follows (untested):
function chr(n) {
if (n < 128) {
return String.fromCharCode(n);
} else {
return "ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ "[n - 128];
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745083443a4610263.html
评论列表(0条)