Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2004
    Posts
    4

    Unanswered: 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));

  2. #2
    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •