Solved

Help in converting a Javascript function to PHP.

Posted on 2004-09-02
2
652 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
ID: 11965129
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
ID: 11965589
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php construct 5 27
PHP is installed but phpmyadmin is not. I decided to get back into linux and I'm a little rusty 5 97
PHP AJAX Wordpress 9 21
PHP Syntax Error 4 27
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
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 …

777 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