If you have xxd
, that's easy: it can convert to and from hexadecimal.
echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181
I don't think there's a reasonable (and reasonably fast) way to convert hexadecimal to binary using only POSIX tools. It can be done fairly easy in Perl. The following script converts hexadecimal to binary, ignoring any input character that isn't a hexadecimal digit. It complains if an input line contains an odd number of hexadecimal digits.
#!/usr/bin/env perl
$^W = 1;
$c = undef;
while (<>) {
tr/0-9A-Fa-f//cd;
if (defined $c) { warn "Consuming $c"; $_ = $c . $_; $c = undef; }
if (length($_) & 1) { s/(.)$//; $c = $1; }
print pack "H*", $_;
}
if (!eof) { die "$!"; }
if (defined $c) { warn "Odd number of hexadecimal digits"; }
If you really need to stick to POSIX (e.g. on an embedded device), I recommend using Base64 instead of hexadecimal. You can use uudecode to decode Base64. The input must have the header format and end line produced by uuencode, it can't be raw Base64.
uudecode <<EOF | nc -l localhost 8181
begin-base64 644 -
AAYwMDAwTkM=
====
EOF
-l
parameter for netcat does mean to listen. It does not send given data, but listen to incoming connection and after connection it will send given data. To send data to some server, just remove-l
parameter.