Solved

Help in converting a Javascript function to PHP.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 …

828 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