Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > convert binary to hex using perl's built in functions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-08-04, 12:16
Dittocat15 Dittocat15 is offline
Registered User
 
Join Date: Jun 2004
Posts: 4
convert binary to hex using perl's built in functions

I'd like to convert a 2000+ character hex string into binary, but when I use perl's built in functions I keep getting extra bits on the end that don't correspond to the hex values.

I'm using

$line = ABCDEF1234.....
$binary = unpack("B*", pack("H*", $line));

Also the same thing is happening with the convers, binary to hex, I get extra hex values.

$line = 01011010...
$hex = unpack("H*", pack ("B*", $line));
Reply With Quote
  #2 (permalink)  
Old 07-10-04, 01:33
senza_nome senza_nome is offline
Registered User
 
Join Date: Jun 2004
Location: Nowhere Near You
Posts: 89
Maybe you could conjure up something like this?
Code:
sub HexToBinary{ my(%h)=('0'=>'0000','1'=>'0001','2'=>'0010','3'=>'0011','4'=>'0100','5'=>'0101','6'=>'0110','7'=>'0111','8'=>'1000','9'=>'1001','A'=>'1010','B'=>'1011','C'=>'1100','D'=>'1101','E'=>'1110','F'=>'1111'); $_=uc $_[0]; s/([0-9A-F])/$h{$1}/g; return $_; };

then use
Code:
my($line_in) = 'ABCDEF1234'; my($line_out)=HexToBinary($line_in);
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On