LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2008, 01:11 PM   #1
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Rep: Reputation: 15
How Can I convert Hex to Binary in a perl script


I'm attempting to write a script that will convert a stream of Hex to a stream of binary using Perl.

I've found a number of code examples such as this:

Code:
%h2b = (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",
);

$hex = "414243";
($binary = $hex) =~ s/(.)/$h2b{lc $1}/g;
print $binary, "\n";
But they are static and seem awfully convoluted to get a result, isn't there a function similar to "bc" at the shell, that can convert a piped "|" input.
 
Old 07-02-2008, 01:18 PM   #2
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
http://search.cpan.org/~eryq/Convert...vert/BinHex.pm
 
Old 07-02-2008, 01:24 PM   #3
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks but that's way past my programing ability, I'm just starting to learn perl.

What I'm trying to do is take an input stream similar to this:

Code:
f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000
And convert it to it's binary equivalent in the same format.
 
Old 07-02-2008, 02:20 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Guide to perl modules: http://stein.cshl.org/genome_informa.../perl-modules/
 
Old 07-02-2008, 02:20 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
#!/bin/bash
echo -n -e $(tr -d '[:space:]' | sed 's/../\\x&/g')
 
Old 07-02-2008, 02:51 PM   #6
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ntubski View Post
Code:
#!/bin/bash
echo -n -e $(tr -d '[:space:]' | sed 's/../\\x&/g')
That does some interesting stuff but not what I was expecting. Instead I get the following:

Code:
$ cat hex.txt
f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000

$ cat hex.txt | ./scripts/hex2bin.sh > hexout.txt

$ cat hexout.txt
���(((^A^B^C^@�<^D_0Y��ጌ^@^@^@^@^@^@^@^@^@^@^@^@^@U^D_0Y파3�7^@^@^@^@^@^@^@ќ<^D_0Y휌꜌ᜌ^@^@^@^@^@^@^@^@^@^@�q�ќ<^D_0Y^@^@^@^@^@^@^@^@^@^@
??
 
Old 07-02-2008, 04:38 PM   #7
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ntubski View Post
Code:
#!/bin/bash
echo -n -e $(tr -d '[:space:]' | sed 's/../\\x&/g')
I'm trying to do it slightly more manually as a bash case statement with output like this:

Code:
while read VALUE; do
    case "$VALUE" in
        0)      echo "0000";;
        1)      echo "0001";;
        2)      echo "0010";;
        3)      echo "0011";;
        4)      echo "0100";;
        5)      echo "0101";;
        6)      echo "0110";;
        7)      echo "0111";;
        8)      echo "1000";;
        9)      echo "1001";;
        A)      echo "1010";;
        B)      echo "1011";;
        C)      echo "1100";;
        D)      echo "1101";;
        E)      echo "1110";;
        F)      echo "1111";;
        a)      echo "1010";;
        b)      echo "1011";;
        c)      echo "1100";;
        d)      echo "1101";;
        e)      echo "1110";;
        f)      echo "1111";;
    esac
done
However when I feed it an input like (f5) it reads it literally as the string f5 instead of the individual (f)and(5).
 
Old 07-02-2008, 04:53 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Try man od
 
Old 07-02-2008, 04:54 PM   #9
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Smile

I got it work! Go me!

The following does the trick:

Code:
#!/usr/bin/perl

$/ = '';

%h2b = (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",
A => "1010", B => "1011", C => "1100",
D => "1101", E => "1110", F => "1111",
);

while (<>) {
        chomp;
                ($binary = $_) =~ s/(.)/$h2b{lc $1}/g;
                print $binary, "\n";
}
If you input something like:

Code:
f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000
You get an output of:

Code:
11110110111101101111011000101000001010000010100000000001000000100000001100000000
10011100001111000000010001011111001100000101100111101101111010101110000110001100
10001100000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000010101010000010001011111001100000101100111101101
00110011111111110011011100000000000000000000000000000000000000000000000000000000
11010001100111000011110000000100010111110011000001011001111011011110101011100001
00000000000000000000000000000000000000000000000000000000000000000000000000000000
10110011011100011010101011010001100111000011110000000100010111110011000001011001
00000000000000000000000000000000000000000000000000000000000000000000000000000000
 
Old 07-02-2008, 05:20 PM   #10
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Now if I can figure out a way to do it in reverse I'll be all set.
 
Old 07-02-2008, 08:38 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
That does some interesting stuff but not what I was expecting.
Yeah, I didn't realize you meant text binary. :P


binary -> hex
Code:
#!/usr/bin/perl

while (<>) {
    chop;
    for (my $i = 0; $i < length($_); $i += 4) {
	my $nibble = '0b'.substr($_, $i, 4);
	printf('%x', oct($nibble));
    }
    print "\n";
}
hex -> binary
Code:
#!/usr/bin/perl
while (<>) {
    chop;
    for (my $i = 0; $i < length($_); $i += 1) {
	printf('%04b', hex(substr($_, $i, 1)))
    }
    print "\n";
}
 
Old 07-02-2008, 08:59 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
can't you use pack and unpack?
 
Old 07-02-2008, 09:10 PM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Yes, here we go.

Code:
#!/usr/bin/perl

use warnings;
use strict;

foreach my $h (qw(0 1 2 e f 10 11 12 1e 1f 20 30 40 50 100 200 1000)) {
        printf "hex = %4s  binary = %16s\n", $h, hex2bin($h);
}


sub hex2bin {
        my $h = shift;
        my $hlen = length($h);
        my $blen = $hlen * 4;
        return unpack("B$blen", pack("H$hlen", $h));
}
Output:
Code:
hex =    0  binary =             0000
hex =    1  binary =             0001
hex =    2  binary =             0010
hex =    e  binary =             1110
hex =    f  binary =             1111
hex =   10  binary =         00010000
hex =   11  binary =         00010001
hex =   12  binary =         00010010
hex =   1e  binary =         00011110
hex =   1f  binary =         00011111
hex =   20  binary =         00100000
hex =   30  binary =         00110000
hex =   40  binary =         01000000
hex =   50  binary =         01010000
hex =  100  binary =     000100000000
hex =  200  binary =     001000000000
hex = 1000  binary = 0001000000000000

Last edited by matthewg42; 07-02-2008 at 09:12 PM. Reason: added output
 
Old 07-02-2008, 09:18 PM   #14
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Wow, there's a lot of ways to do it in perl...

Cool, I guess I'll time the processes and see what ones the most time efficient and go from there.

Thanks everyone.
 
Old 01-12-2010, 03:36 AM   #15
davitz38
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
Hi guys,
For some of you still interested in converting hexadecimal to binary, here's a hex to binary converter. You can make sure you are converting the right way using this tool
David
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert characters to hex values in perl pjz Programming 3 12-08-2005 08:35 AM
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 09:30 PM
calling a c++ binary inside a perl script Blue_muppet Programming 3 08-28-2004 11:31 PM
Convert from shell script to binary? Anon123 Linux - General 4 06-26-2004 05:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration