Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is equivalent php chr() and ord() functions in javascript

Tags:

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

like image 403
vineet Avatar asked Oct 18 '16 05:10

vineet


People also ask

What is CHR in JavaScript?

Compile and run Constraint Handling Rules (CHR) in JavaScript. CHR. js is a just-in-time (JIT) compiler for Constraint Handling Rules, embedded in JavaScript. For better runtime performance it supports ahead-of-time (AOT) compilation too, either by its command line tool chrjs or babel-plugin-chr, a plugin for Babel.

What is Ord function in PHP?

PHP | ord() Function The ord() function is a inbuilt function in PHP that returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string. Syntax: int ord($string)

What is the use of Chr function in PHP?

PHP chr() Function The chr() function returns a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. Octal values are defined by a leading 0, while hex values are defined by a leading 0x.


1 Answers

var res = String.fromCharCode(65); 

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

like image 170
Mayur Aglawe Avatar answered Oct 21 '22 04:10

Mayur Aglawe