Hex to Octal (3 digit) converer
Eric Smith
spacewar at gmail.com
Mon May 4 00:07:24 CDT 2015
> 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.
With python 2.7 installed, the following program, which I call
hex2oct.py, will do it:
#!/usr/bin/python2
# Copyright 2015 Eric Smith <spacewar at gmail.com>
# Released under Creative Commons BY-SA 4.0 International Public License
# https://creativecommons.org/licenses/by-sa/4.0/legalcode
import fileinput,re,sys
r = re.compile('[0-9A-F]+', re.I)
for line in fileinput.input():
while len(line):
m = r.search(line)
if m:
sys.stdout.write(line[:m.start()])
sys.stdout.write("%03o" % int(m.group(),16))
line = line[m.end():]
else:
sys.stdout.write(line)
line = ''
Just put that in a file and chmod it to be executable. Change the path
in the first line to the location of your python executable.
(Alternatively, put "python" or the full path to the python executable
on the command line before then name of this program.)
By default It takes data from standard input, or you can specify one
or more filenames on the command line to be processed sequentially.
>From standard input, enter as many lines of input as you like, then an
end-of-file (control-D on Unix and derivatives, control-Z on Windows,
though I haven't tested it on Windows). Exit by an empty line with an
end-of-file.
Any non-hexadecimal data before, between, or after hexadecimal numbers
is preserved, so you can have commas, tabs, etc. in the input and get
them back in the output.
Hexadecimal values larger than FF will likely yield undesired results.
The program could be easily modified to ignore them, complain about
them, or automatically split up sequences of more than 2 hex digits.
More information about the cctech
mailing list