Skip to content

Instantly share code, notes, and snippets.

@dex4er
Created February 8, 2012 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dex4er/1773961 to your computer and use it in GitHub Desktop.
Save dex4er/1773961 to your computer and use it in GitHub Desktop.
hexcodec
#!/usr/bin/perl
use strict;
use IPC::Open2;
use IO::Select;
use IO::Handle;
use constant {
BUFLEN => 1024 * 4,
TIMEOUT => 0.1,
};
sub bin2hex {
my ($buf) = @_;
return uc(unpack "H*", $buf) . "\n";
};
my $tail = '';
sub hex2bin {
my ($buf) = @_;
my @ret;
foreach my $block (split "\n", "$tail$buf") {
if (length($block) % 2) {
$tail = substr $block, -1;
}
else {
$tail = '';
}
push @ret, pack "H*", substr($block, 0, length($block) - length($tail));
};
return join '', @ret;
};
die "Usage: $0 cmd\n" unless @ARGV;
my ($stdin, $stdout) = (\*STDIN, \*STDOUT);
my $pid = open2(my $pipein, my $pipeout, @ARGV);
my $selector = IO::Select->new($stdin, $pipein);
my $eof = 0;
while (not $eof) {
my @ready = $selector->can_read(TIMEOUT);
foreach my $rh (@ready) {
if ($rh == $stdin) {
my $size = sysread($rh, my $buf, BUFLEN) or $eof = 1;
syswrite($pipeout, $buf, length $buf);
}
elsif ($rh == $pipein) {
my $size = sysread($rh, my $buf, BUFLEN) or $eof = 1;
syswrite($stdout, $buf, length $buf);
}
};
};
my $selector = IO::Select->new($pipein);
while ($selector->can_read(TIMEOUT)) {
my $size = sysread($pipein, my $buf, BUFLEN);
syswrite($stdout, $buf, length $buf);
};
#!/usr/bin/perl
use strict;
use IPC::Open2;
use IO::Select;
use IO::Handle;
use constant {
BUFLEN => 1024 * 4,
TIMEOUT => 0.1,
};
sub bin2hex {
my ($buf) = @_;
return uc(unpack "H*", $buf) . "\n";
};
my $tail = '';
sub hex2bin {
my ($buf) = @_;
my @ret;
foreach my $block (split "\n", "$tail$buf") {
if (length($block) % 2) {
$tail = substr $block, -1;
}
else {
$tail = '';
}
push @ret, pack "H*", substr($block, 0, length($block) - length($tail));
};
return join '', @ret;
};
die "Usage: $0 cmd\n" unless @ARGV;
my ($stdin, $stdout) = (\*STDIN, \*STDOUT);
my $pid = open2(my $pipein, my $pipeout, @ARGV);
my $selector = IO::Select->new($stdin, $pipein);
my $eof = 0;
while (not $eof) {
my @ready = $selector->can_read(TIMEOUT);
foreach my $rh (@ready) {
if ($rh == $stdin) {
my $size = sysread($rh, my $buf, BUFLEN) or $eof = 1;
my $out = bin2hex $buf;
syswrite($pipeout, $out, length $out);
}
elsif ($rh == $pipein) {
my $size = sysread($rh, my $buf, BUFLEN) or $eof = 1;
my $out = hex2bin $buf;
syswrite($stdout, $out, length $out);
}
};
};
my $selector = IO::Select->new($pipein);
while ($selector->can_read(TIMEOUT)) {
my $size = sysread($pipein, my $buf, BUFLEN);
my $out = hex2bin $buf;
syswrite($stdout, $out, length $out);
};
#!/usr/bin/perl
use strict;
use IPC::Open2;
use IO::Select;
use IO::Handle;
use constant {
BUFLEN => 1024 * 4,
TIMEOUT => 0.1,
};
sub bin2hex {
my ($buf) = @_;
return uc(unpack "H*", $buf) . "\n";
};
my $tail = '';
sub hex2bin {
my ($buf) = @_;
my @ret;
foreach my $block (split "\n", "$tail$buf") {
if (length($block) % 2) {
$tail = substr $block, -1;
}
else {
$tail = '';
}
push @ret, pack "H*", substr($block, 0, length($block) - length($tail));
};
return join '', @ret;
};
die "Usage: $0 cmd\n" unless @ARGV;
my ($stdin, $stdout) = (\*STDIN, \*STDOUT);
my $pid = open2(my $pipein, my $pipeout, @ARGV);
my $selector = IO::Select->new($stdin, $pipein);
my $eof = 0;
while (not $eof) {
my @ready = $selector->can_read(TIMEOUT);
foreach my $rh (@ready) {
if ($rh == $stdin) {
my $size = sysread($rh, my $buf, BUFLEN) or $eof = 1;
my $out = hex2bin $buf;
syswrite($pipeout, $out, length $out);
}
elsif ($rh == $pipein) {
my $size = sysread($rh, my $buf, BUFLEN) or $eof = 1;
my $out = bin2hex $buf;
syswrite($stdout, $out, length $out);
}
};
};
my $selector = IO::Select->new($pipein);
while ($selector->can_read(TIMEOUT)) {
my $size = sysread($pipein, my $buf, BUFLEN);
my $out = bin2hex $buf;
syswrite($stdout, $out, length $out);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment