I have been working on an encoding/decoding project for my site. I want to offer my users the choice between base64 and hex encoding/decoding. In PHP, both base64 encode/decode work fine and bin2hex works fine as well. But, when I try to use hex2bin in PHP, I get an internal server error. I've check my error and it says PHP Fatal error: Call to undefined function hex2bin().
I recently did a PHP to JavaScript conversion of both base64 and hex encoding/decoding. I have the Javascript for the hex below. I tried to convert it back to PHP but I'm at a loss on the 10th line with charCodeAt and toString. I was wondering if anybody might be able to take this code and do a Javascript to PHP conversion for me. What I mean to say is that I need a way to do bin2hex() and hex2bin() in PHP without having to actually use these two functions.
If it looks like it can't be done, does anybody know of any other encoding/decoding functions in PHP that I can use instead of the hex functions.Code:var Hex = { // PHP to JavaScript bin2hex encode : function(input) { // Replace tab characters with spaces and end-of-line markers with '~EOL' input = _str_replace([new RegExp("\t","g"), new RegExp("\n","g")], [" ", "~EOL"], input); var r = ''; var i = 0; var h; while(i < input.length) { h = input.charCodeAt(i++).toString(16); while(h.length < 2) { h = h; } r += h; } return r; }, // PHP to JavaScript hex2bin decode : function(input) { var r = ''; for(var i = 0; i < input.length; i += 2) { r += unescape('%'+input.substr(i, 2)); } // Change back end-of-line markers r = _str_replace(new RegExp("~EOL", "g"), "\n", r); return r; }, is_hex:function(str) { var check; if(str.length%2 != 0) { return false; } for(var i = 0; i < str.length; i++) { check = str[i].charCodeAt(); // 0 9 a f A F if((check < 48 || check > 57) && (check < 97 || check > 102) && (check < 65 || check > 70)) { return false; } } return true; } };


Reply With Quote


Bookmarks