Solved

Help in converting a Javascript function to PHP.

Posted on 2004-09-02
2
669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

626 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