Solved

Help in converting a Javascript function to PHP.

Posted on 2004-09-02
2
633 Views
Last Modified: 2008-01-09
Hi all,

i have the following function taht I tried to convert to PHP, but I faild because I'm not fully aware of Javascript Syntax, it basically converts a string of HEX numbers into a normail string.

function hexToString (h) {
  var r = "";
  for (var i= (h.substr(0, 2)=="0x")?2:0; i<h.length; i+=2) {r += String.fromCharCode (parseInt (h.substr (i, 2), 16));}
  return r;
}
this is basically a reverse of

function stringToHex (s) {
  var r = "0x";
  var hexes = new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  for (var i=0; i<s.length; i++) {r += hexes [s.charCodeAt(i) >> 4] + hexes [s.charCodeAt(i) & 0xf];}
  return r;
}

Which is in PHP:

function stringToHex ($s) {
  $r = "0x";
  $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  for ($i=0; $i<strlen($s); $i++) {$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);}
  return $r;
}

Any help would be great.
0
Comment
Question by:kalmen
2 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
Comment Utility
the String to hex is implemented in PHP with the function

bin2hex

In the comments you also find the function to convert hex2bin:
http://de3.php.net/manual/en/function.bin2hex.php#34136

function hex2bin($hexdata){
for( $i = 0; $i < strlen( $hexdata ); $i += 2 )
eval( '$bindata .= "\x' . substr( $hexdata, $i, 2 ) . '";' );
return $bindata;}
0
 
LVL 1

Author Comment

by:kalmen
Comment Utility
Hi,

I used these two functions from the link you provided:
function stringToHex ($s) {
   $len = strlen($s);
   for ($i=0; $i<$len; $i++) $data.=sprintf("%02x",ord(substr($s,$i,1)));
   return $data;
}

function hexToString ($h) {
   $len = strlen($h);
   for ($i=0;$i<$len;$i+=2) $data.=chr(hexdec(substr($h,$i,2)));
   return $data;
}
My problem is here,

My Javascript functions do not work the same way, I'm trying to implement the exact reverse of string2Hex in JS  as HextoString in PHP.

Now, my Javascript function string2Hex encodes "wazzap?" as 0x77617a7a61703f
and my PHP hextostring function encodes "wazzap? as 77617a7a61703f ..

Ooops.. I just noticed the 0x... My bad.. I'll just add it and see how it goes.

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now