Why does charCodeAt in Javascript seem to behave differently from PHP's chr? I'm trying to implement base64
<p>I have used bitwise operator in php code which return decode string in base64. I want implement that php code same as in javascript. As per my knowledge <a href="https://www.php.net/manual/en/function.chr.php" rel="nofollow noreferrer"><code>chr()</code></a> equivalent to <code>String.fromCharCode(n)</code>  and <a href="https://www.php.net/manual/en/function.ord.php" rel="nofollow noreferrer"><code>ord()</code></a> is <code>n.charCodeAt(0)</code>. But both final output are differed.</p>
<p><em><strong>PHP code:-</strong></em></p>
<pre><code>&lt;?php
$pass = &quot;RuvEtrUt74gaDR5DufuChe&quot;;
$en = &quot;&quot;;
foreach(str_split($pass) as $chr){
    $b1=((($chr = ord($chr)) &gt;&gt; 1) &amp; 0xFF);
    $b2=($chr &lt;&lt; (8 - 1));
     $en = $en.chr( $b1|$b2 );
}
$en = base64_encode($en); //Output:- )º;¢:9ª:›³°&quot;)š&quot;º3º¡4²
echo ($en);
echo (base64_decode($en)); //Output:- Kbo7ojo5qjqbGrOwIimaIrozuqE0sg==
</code></pre>
<p><em><strong>In Javascript Code:-</strong></em></p>
<pre><code>var pass = &quot;RuvEtrUt74gaDR5DufuChe&quot;;
var en = &quot;&quot;;
var passArr = pass.split('');
for (var i = 0; i &lt; passArr.length; i++) {
    var b1 = (((passArr[i] = passArr[i].charCodeAt(0)) &gt;&gt; 1) &amp; 0xFF);
    var b2 = (passArr[i] &lt;&lt; (8 - 1));
    en += chr(b1 | b2);
}
console.log('en',en);//Output:- ⤩㪺㬻⊢㨺㤹⪪㨺ᮛᨚ㎳グ∢⤩᪚∢㪺㌳㪺↡㐴㊲
en = window.btoa(unescape(encodeURIComponent(en)));
console.log('en', en);//Output:- 4qSp46q646y74oqi46i646S54qqq46i64a6b4aia446z44Kw4oii4qSp4aqa4oii46q644yz46q64oah45C044qy
                     //need same as in php i.e, :- Kbo7ojo5qjqbGrOwIimaIrozuqE0sg==
function chr(codePt) {
    if (codePt &gt; 0xFFFF) {
        codePt -= 0x10000
        return String.fromCharCode(0xD800 + (codePt &gt;&gt; 10), 0xDC00 + (codePt &amp; 0x3FF))
    }
    return String.fromCharCode(codePt)
}
</code></pre>
<p>As you can see above javscript output is differed from php output. I need exact output in javascript which return in php code. Thanks</p>