Hex to Octal (3 digit) converer

Philip Lord philip at neoncluster.com
Mon May 4 02:23:23 CDT 2015


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


> On May 4, 2015, at 5:58 PM, Don North <north at alum.mit.edu> wrote:
> 
> On 5/3/2015 9:31 PM, Philip Lord wrote:
>> Hi,
>> I’m looking for a program (or preferably an online conversion site, as I use Macintosh) that can convert a long stream of Hex, to 3 digit Octal.
>> I have found one site that works ok (http://www.kjetil-hartveit.com/blog/10/hex-binary-decimal-octal-and-ascii-converter <http://www.kjetil-hartveit.com/blog/10/hex-binary-decimal-octal-and-ascii-converter> <http://www.kjetil-hartveit.com/blog/10/hex-binary-decimal-octal-and-ascii-converter <http://www.kjetil-hartveit.com/blog/10/hex-binary-decimal-octal-and-ascii-converter>>), but the octal it outputs is not always 3 digits long. Unfortunately I then need to manually add the missing ‘0’s, which can be a huge pain for long listings, and open to human error.
>> 
>> For example the above site does….
>> 
>> Entering this:
>> 0E 09 11 1B 01 CD 05 00
>> 
>> Outputs this:
>> 16 11 21 33 1 315 5 0
>> 
>> But I want this:
>> 016 011 021 033 001 315 005 000
>> 
>> Does anyone know of a good site (or some good software)?
>> 
>> Much thanks
>> Phil
> 
> Here is a perl one-liner that does what you want:
> 
> perl -n -e 'print join(" ",map(sprintf("%03o",hex($_)),split(" ")))."\n";'
> 
> Example:
> 
> ~[514] perl -n -e 'print join(" ",map(sprintf("%03o",hex($_)),split(" ")))."\n";'
> 1 2 3 de ad be
> 001 002 003 336 255 276
> 
> I typed the red line as input data, the program output the blue line.
> You can also use standard unix file indirection or piping as well on the program line.
> 
> Don



More information about the cctech mailing list