Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Convert Hex to Binary in Perl?

Status
Not open for further replies.

davyzhu

Advanced Member level 1
Joined
May 23, 2004
Messages
494
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,298
Location
oriental
Activity points
4,436
perl convert hex to binary

Hello all,

I want to write a perl script to convert hex data to binary,
the command maybe "perl hex2bin.pl hex_file bin_file".

E.g. The hex_file may be:
1234
89ab

and the converted bin_file will be:
0001001000110100
1000100110101011

Is it possible? Thanks!

DAVY
 

perl hex2bin

i think perl is not suitable for math programming. it's purpose is to ease text programming or changing.for example it is so good to use perl for machim programming(programs which are produced with computer itself)
 

perl hex to binary

Look at the printf command in perl.


LLCD
 

hex2bin perl

mhamed said:
i think perl is not suitable for math programming. it's purpose is to ease text programming or changing.for example it is so good to use perl for machim programming(programs which are produced with computer itself)


Hi
I think just in 180 degrees unlike you.
Please look only at the following two items I mentioned you,

1. Look at "mastering Algorithms in/with perl" book
2. Perl has an enriched backend called CPAN look/search at CPAN.ORG to find anything you think/need. It is wonderful!!


BTW: I think you must use any thing in its correct place. I agree that the speed of perl is not comparable of C/C++, but algorithm implementation in perl is very simpler than C/C++.


tnx
 

perl hex to bin

Hello all,

I have complete the simple perl code.

The Hex file and bin file all in ASCII mode.

Code:
#Convert Hex file to bin file

# cmd	module	  input	output	continous endian
# perl hex2bin.pl hex.f bin.f	0/1		0/1
#	ARGV[0]	ARGV[1] ARGV[2]   ARGV[3]

#typical usage
# perl hex2bin.pl hex.f bin.f 0 1

#open input hex file
$file = $ARGV[0];
open(inF, $file) or dienice ("file open failed\n"); 
@lines = <inF>;                                    
close(inF); 

#open output bin file
open(OUTPUT, ">$ARGV[1]") or die "ERROR : Cannot open $ARGV[1].\n";

#default not continous (1:continous 0: with "\n")
$continous = $ARGV[2];

#default big endian (1: big endian 0:little endian)
$endian = $ARGV[3];

#convert hex to bin                                      
foreach $line (@lines){
	chomp $line;
	@chars = split(//,$line);
		foreach $char (@chars){
			if ($char eq "0"){
				print OUTPUT "0000";
			}
			elsif ($char eq "1"){
				($endian == 1) 
					? 
					(print OUTPUT "0001")
					:
					(print OUTPUT "1000");
			}
			elsif ($char eq "2"){
				($endian == 1) 
					? 
					(print OUTPUT "0010")
					:
					(print OUTPUT "0100");
			}
			elsif ($char eq "3"){
				($endian == 1) 
					? 
					(print OUTPUT "0011")
					:
					(print OUTPUT "1100");
			}
			elsif ($char eq "4"){
				($endian == 1) 
					? 
					(print OUTPUT "0100")
					:
					(print OUTPUT "0010");
			}
			elsif ($char eq "5"){
				($endian == 1) 
					? 
					(print OUTPUT "0101")
					:
					(print OUTPUT "1010");
			}
			elsif ($char eq "6"){
					print OUTPUT "0110";

			}
			elsif ($char eq "7"){
				($endian == 1) 
					? 
					(print OUTPUT "0111")
					:
					(print OUTPUT "1110");
			}
			elsif ($char eq "8"){
				($endian == 1) 
					? 
					(print OUTPUT "1000")
					:
					(print OUTPUT "0001");
			}
			elsif ($char eq "9"){
					print OUTPUT "1001";
			}			
			elsif (($char eq "a")|| ($char eq "A")){
				($endian == 1) 
					? 
					(print OUTPUT "1010")
					:
					(print OUTPUT "0101");
			}		
			elsif ($char eq "b" || $char eq "B"){
				($endian == 1) 
					? 
					(print OUTPUT "1011")
					:
					(print OUTPUT "1101");
			}		
			elsif ($char eq "c" || $char eq "C"){
				($endian == 1) 
					? 
					(print OUTPUT "1100")
					:
					(print OUTPUT "0011");
			}		
			elsif ($char eq "d" || $char eq "D"){
				($endian == 1) 
					? 
					(print OUTPUT "1101")
					:
					(print OUTPUT "1011");
			}		
			elsif ($char eq "e" || $char eq "E"){
				($endian == 1) 
					? 
					(print OUTPUT "1110")
					:
					(print OUTPUT "0111");
			}		
			elsif ($char eq "f" || $char eq "F"){
				print OUTPUT "1111";
			}	
			#other charactor will be replaced by a space
			else {
				print OUTPUT " ";
			}		
		}
	if ($continous == 0)
		{print OUTPUT "\n"};
}

#show error message (useless)
sub dienice {
	my($errmsg) = @_;
	print"$errmsg\n";
	exit;
}

DAVY
 

convert hex to binary

Wow. How about this? Given a value in $line you want to convert:

Code:
my %conv = qw(	0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 
				a 10 b 11 c 12 d 13 e 14 f 15 
				A 10 B 11 C 12 D 13 E 14 F 15 );

my $num = 0;
map { $num *= 16; $num += $conv{$_}; } split //, $line;

$num now has the value.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top