vineet
vineet

Reputation: 14236

Why does charCodeAt in Javascript seem to behave differently from PHP's chr? I'm trying to implement base64

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 chr() equivalent to String.fromCharCode(n) and ord() is n.charCodeAt(0). But both final output are differed.

PHP code:-

<?php
$pass = "RuvEtrUt74gaDR5DufuChe";
$en = "";
foreach(str_split($pass) as $chr){
    $b1=((($chr = ord($chr)) >> 1) & 0xFF);
    $b2=($chr << (8 - 1));
     $en = $en.chr( $b1|$b2 );
}
$en = base64_encode($en); //Output:- )º;¢:9ª:›³°")š"º3º¡4²
echo ($en);
echo (base64_decode($en)); //Output:- Kbo7ojo5qjqbGrOwIimaIrozuqE0sg==

In Javascript Code:-

var pass = "RuvEtrUt74gaDR5DufuChe";
var en = "";
var passArr = pass.split('');
for (var i = 0; i < passArr.length; i++) {
    var b1 = (((passArr[i] = passArr[i].charCodeAt(0)) >> 1) & 0xFF);
    var b2 = (passArr[i] << (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 > 0xFFFF) {
        codePt -= 0x10000
        return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF))
    }
    return String.fromCharCode(codePt)
}

As you can see above javscript output is differed from php output. I need exact output in javascript which return in php code. Thanks

Upvotes: 30

Views: 36173

Answers (4)

Basic Block
Basic Block

Reputation: 781

I think there is a cleaner way to do this.

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];
}

Note that a few weird characters like \18 somehow has a string length of 2 in javascript, so in order to make indexing work I replaced these with \0 which has length 1

Upvotes: 0

Jessie Lesbian
Jessie Lesbian

Reputation: 1486

this code is the javascript equivalent to the PHP ord() function.

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

Upvotes: 8

Mayur Aglawe
Mayur Aglawe

Reputation: 481

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

and for ord() you can use the above function for JavaScript

Upvotes: 17

Mayur Aglawe
Mayur Aglawe

Reputation: 481

var res = String.fromCharCode(65);

This function will worked well same as chr() function returns character in javascript.

Upvotes: 31

Related Questions