javascript中等效的php chr()和ord()函数是什么

javascript中等效的php chr()和ord()函数是什么,javascript,encoding,base64,bitwise-operators,Javascript,Encoding,Base64,Bitwise Operators,我在php代码中使用了按位运算符,它在base64中返回解码字符串。我希望实现与javascript中相同的php代码。据我所知,chr()相当于String。fromCharCode(n)和ord()是n.charCodeAt(0)。但这两种最终结果是不同的 PHP代码:- 此函数的工作原理与chr()函数在javascript中返回字符的工作原理相同 function myFunction() { var str = "HELLO WORLD"; var n = str.charCodeA

我在php代码中使用了按位运算符,它在base64中返回解码字符串。我希望实现与javascript中相同的php代码。据我所知,
chr()
相当于
String。fromCharCode(n)
ord()
n.charCodeAt(0)
。但这两种最终结果是不同的

PHP代码:-


此函数的工作原理与
chr()
函数在javascript中返回字符的工作原理相同

function myFunction() {
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
    document.getElementById("demo").innerHTML = n;
}

对于ord(),您可以将上述函数用于JavaScript,这段代码是与PHP ord()函数等价的JavaScript

function ord(str){return str.charCodeAt(0);}

我认为有一种更干净的方法可以做到这一点

const ascii_table = "\0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\0\0\20\21\22\23\24\25\26\27\0\0\30\31\32\33\34\35\36\37\0\0\40\41\42\43\44\45\46\47\0\0\50\51\52\53\54\55\56\57\0\0\60\61\62\63\64\65\66\67\0\0\70\71\72\73\74\75\76\77\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\100\101\102\103\104\105\106\107\0\0\110\111\112\113\114\115\116\117\0\0\120\121\122\123\124\125\126\127\0\0\130\131\132\133\134\135\136\137\0\0\140\141\142\143\144\145\146\147\0\0\150\151\152\153\154\155\156\157\0\0\160\161\162\163\164\165\166\167\0\0\170\171\172\173\174\175\176\177\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\200\201\202\203\204\205\206\207\0\0\210\211\212\213\214\215\216\217\0\0\220\221\222\223\224\225\226\227\0\0\230\231\232\233\234\235\236\237\0\0\240\241\242\243\244\245\246\247\0\0\250\251\252\253\254\255";

function chr(chr){
  return ascii_table.indexOf(chr);
}

function ord(index){
  return ascii_table[index];
}

请注意,一些奇怪的字符,如\18,在javascript中的字符串长度为2,因此为了使索引工作正常,我将其替换为长度为1的\0,当arg为负时如何使用此函数。例如,chr(-77)返回� 在PHP中。如何在javascript中实现它?js'\45'==py-chr(35)==chr(0x23)
function myFunction() {
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
    document.getElementById("demo").innerHTML = n;
}
function ord(str){return str.charCodeAt(0);}
const ascii_table = "\0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\0\0\20\21\22\23\24\25\26\27\0\0\30\31\32\33\34\35\36\37\0\0\40\41\42\43\44\45\46\47\0\0\50\51\52\53\54\55\56\57\0\0\60\61\62\63\64\65\66\67\0\0\70\71\72\73\74\75\76\77\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\100\101\102\103\104\105\106\107\0\0\110\111\112\113\114\115\116\117\0\0\120\121\122\123\124\125\126\127\0\0\130\131\132\133\134\135\136\137\0\0\140\141\142\143\144\145\146\147\0\0\150\151\152\153\154\155\156\157\0\0\160\161\162\163\164\165\166\167\0\0\170\171\172\173\174\175\176\177\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\200\201\202\203\204\205\206\207\0\0\210\211\212\213\214\215\216\217\0\0\220\221\222\223\224\225\226\227\0\0\230\231\232\233\234\235\236\237\0\0\240\241\242\243\244\245\246\247\0\0\250\251\252\253\254\255";

function chr(chr){
  return ascii_table.indexOf(chr);
}

function ord(index){
  return ascii_table[index];
}