PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Feb 24, 2018 9:24 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: bin2hex / hex2bin help
PostPosted: Wed Aug 25, 2004 11:07 am 
Offline
Forum Newbie

Joined: Wed Aug 25, 2004 11:07 am
Posts: 2
I'm building a system which will allow me to encrypt and store data in a MySQL DB. The encrypting and decrypting parts I have no problems with, however in order to store the number in the DB I need to perform a bin2hex conversion.

I am using a hex2bin function that I found somewhere on PHP Builder, to convert the stored, encrypted number back to hex, however when I perform these functions I am only getting half of the number converted (the first 8 characters). I am posting the code, any guidance or help is greatly appreciated.

Syntax: [ Download ] [ Hide ]
<?php

//convert hex string to binary string (used in decrypt_data)

function hex2bin($data) {

    $len = strlen($data);

    for($i=0;$i<$len;$i+=2) {

        $newdata .= pack("C",hexdec(substr($data,$i,2)));

    }

    return $newdata;

}

?>



thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 12:02 pm 
Offline
Forum Regular
User avatar

Joined: Fri May 09, 2003 11:53 pm
Posts: 589
Location: Belo Horizonte, Brazil
It seems to be working:
Syntax: [ Download ] [ Hide ]
<?php

function hex2bin($data) {

        $len = strlen($data);

        $newdata = '';

        for($i=0;$i<$len;$i+=2) {

                $newdata .= pack("C",hexdec(substr($data,$i,2)));

        }

        return $newdata;

}



$str = "Test bit of string!";

$hexstr = bin2hex($str);

echo hex2bin($hexstr) . "\n";

echo pack("H*", $hexstr) . "\n";// maybe a simpler way

?>

output:
Syntax: [ Download ] [ Hide ]
Test bit of string!
Test bit of string!

-- Scorphus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 12:43 pm 
Offline
Forum Newbie

Joined: Wed Aug 25, 2004 11:07 am
Posts: 2
Thanks for the quick reply scorphus. You're right, the hex2bin function seems to be working properly. I guess the error must be occurring during either my encrypt or decrypt functions.

I'm a bit cautious about posting my encrypt/decrypt codes but if anyone had any ideas why only the first 8 characters of a 16 character string are being decrypted properly I'm very open to ideas.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group