All pastes #1585999 Raw Edit

hex2bin.pl

public text v1 · immutable
#1585999 ·published 2009-09-30 13:54 UTC
rendered paste body
#!/usr/bin/perl
# hex2bin.pl - convert hexadecimal data to binary data or inverse

if (grep(/^-.*i/, @ARGV)) {
    $inverse = 1;
}
if (grep(/^-.*n/, @ARGV)) {
    $through_newline = 1;
}

#if ($0 eq 'hex2bin.pl') {
if (!$inverse) {
    while (read(STDIN, $data, 1)) {
	if ($data =~ /[0-9a-fA-F]/) {
	    if ($upper ne '') {
		printf('%c', hex($upper.$data));
		$upper = '';
	    } else {
		$upper = $data;
	    }
	} elsif($through_newline && $data eq "\n") {
	    print $data;
	}
    }
} else {
    while (read(STDIN, $data, 1)) {
	if($through_newline && $data eq "\n") {
	    print $data;
	    next;
	}
	printf('%02x', ord $data);
    }
}