Hex to Octal (3 digit) converer

Don North north at alum.mit.edu
Mon May 4 00:58:54 CDT 2015


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>), 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