Solved

Help in converting a Javascript function to PHP.

Posted on 2004-09-02
2
665 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

732 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