Hex to Octal (3 digit) converer

Don North north at alum.mit.edu
Mon May 4 02:54:24 CDT 2015


On 5/4/2015 12:23 AM, Philip Lord wrote:
> Thanks again, I’ve never used Perl either, but I just installed it.
>
> So I saved your script as a textfile called HEX_OCTAL.pl:
>
> perl -n -e 'print join(" ",map(sprintf("%03o",hex($_)),split(" ")))."\n";'
>
> But unfortunately I get the following error when I try to run it:
>
> syntax error at /Users/Philip/perl5/perlbrew/Perl_tests/HEX_OCTAL.pl line 1, near "n -e "
> Execution of /Users/Philip/perl5/perlbrew/Perl_tests/HEX_OCTAL.pl aborted due to compilation errors.
>
> Not sure what I’m doing wrong…being a noob, it could be anything!
>
> Phil
>

There are two ways to resolve this.

First, you just put the command line I gave you into a shell script file (call 
it conv.sh):

     #!/bin/bash
     perl -n -e 'print join(" ",map(sprintf("%03o",hex($_)),split(" ")))."\n";'

Then just type './conv.sh' on the command line to run in interactive mode, or 
'./conv.sh < test.dat' to source data from a file test.dat.

Or, second, to turn the perl one-liner into a real perl program, it needs to be 
this (call it conv.pl):

     #!/usr/bin/perl
     while (<>) { print join(" ",map(sprintf("%03o",hex($_)),split(" ")))."\n"; }
     exit;

then you can run this in the same way as above, except type './conv.pl' or 
'./conv.pl < test.dat'

Don




More information about the cctech mailing list