JavaScript equivalent of PHP's chr() function - Stack Overflow

How to transform the following code from PHP to JavaScript?$str = '';for ($i = 0; $i < 25

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.

Share Improve this question edited Jun 20, 2016 at 17:11 le_m 20.3k10 gold badges69 silver badges78 bronze badges asked May 23, 2016 at 16:23 ADiADi 372 silver badges4 bronze badges 4
  • 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
Add a ment  | 

1 Answer 1

Reset to default 7

PHP'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

相关推荐

  • JavaScript equivalent of PHP&#39;s chr() function - Stack Overflow

    How to transform the following code from PHP to JavaScript?$str = '';for ($i = 0; $i < 25

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信