DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    17

    PHP: encoding/decoding

    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.

    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;
        }
    };
    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.

  2. #2
    Join Date
    Feb 2010
    Posts
    17
    I think I got a pretty good substitue here, it's not an exact conversion but it works fine for me.

    I found it after combing the long reaches of the internet and I'm glad I did. Here is the link: http://fiddyp.co.uk/code-to-reverse-a-bin2hex-function/. I'll also post the code here as well.
    PHP Code:
    function hex2bin($data) {
        
    //function URL: http://fiddyp.co.uk/code-to-reverse-a-bin2hex-function/
        //function author: Andy Bailey
        
    $len strlen($data);
        for(
    $i=0;$i<$len;$i+=2) {
            
    $newdata .= pack("C",hexdec(substr($data,$i,2)));
        }
        return 
    $newdata;

    I've tested the code many times and it works just fine every time.

Similar Threads

  1. glb decoding
    By jkmcgrath in forum VB Classic
    Replies: 0
    Last Post: 06-21-2004, 08:19 PM
  2. encoding/decoding
    By anonymous in forum VB Classic
    Replies: 0
    Last Post: 01-10-2004, 06:03 AM
  3. decoding utf-8 in asp
    By anonymous in forum VB Classic
    Replies: 0
    Last Post: 07-28-2003, 12:41 AM
  4. Decoding.....
    By matt in forum Architecture and Design
    Replies: 0
    Last Post: 03-14-2002, 12:37 PM
  5. DVD Decoding
    By Pickens in forum VB Classic
    Replies: 1
    Last Post: 07-15-2000, 12:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links