## Monday, October 10, 2016

### PDF cheat sheet: bin/oct/dec/hex conversion (0-255)

By Vasudev Ram

Numeral system image attribution

Hi readers,

Here is another in my series of PDF-generation applications built using xtopdf, my Python toolkit for PDF creation from various data formats.

This program generates a PDF cheat sheet for conversion of the numbers 0 to 255 between binary, octal, decimal and hexadecimal numeral systems. It can be useful for programmers, electrical / electronics engineers, scientists or anyone else who has to deal with numbers in those bases.

Here is the program, in file number_systems.py:
```from __future__ import print_function
from PDFWriter import PDFWriter
import sys

'''
A program to generate a table of numbers from
0 to 255, in 4 numbering systems:
- binary
- octal
- decimal
- hexadecimal
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store on Gumroad: https://gumroad.com/vasudevram
'''

def print_and_write(s, pw):
print(s)
pw.writeLine(s)

sa, lsa = sys.argv, len(sys.argv)
if lsa == 1:
sys.stderr.write("Usage: {} out_filename.pdf\n".format(sa[0]))
sys.exit(1)

with PDFWriter(sa[1]) as pw:

pw.setFont('Courier', 12)
pw.setHeader('*** Number table: 0 to 255 in bases 2, 8, 10, 16 ***')
pw.setFooter('*** By xtopdf: https://google.com/search?q=xtopdf ***')
b = "Bin"; o = "Oct"; d = "Dec"; h = "Hex"
header = "{b:>10}{o:>10}{d:>10}{h:>10}".format(b=b, o=o, d=d, h=h)

for i in range(256):
if i % 16 == 0:
print_and_write(header, pw)
print_and_write("{b:>10}{o:>10}{d:>10}{h:>10}".format( \
b=bin(i), o=oct(i), d=str(i), h=hex(i)), pw)

print_and_write(header, pw)
```
And here is a screenshot of first page of the PDF output, after running the program
with the command: python number_systems.py BODH-255.pdf

Ans here is a screenshot of the last page:

You can get the cheat sheet from my Gumroad store here: gum.co/BODH-255.

(You can also get email updates about my future products.)

I named the output BODH-255.pdf (from Binary Decimal Octal Hexadecimal 255), instead of BODH-0-255.pdf, because, of course, programmers count from zero, so the 0 can be implicit :)

(The formatting of the output is a little unconventional, due to the use of the header line occurring after every 16 lines in the table, but it is on purpose, because I am experimenting with different formats, to see the effect on readability / usability).

Notice 1) the smooth and systematic progression of the numbers in the vertical columns (how the values change like a car's odometer), and also 2) the relationships between numbers in different columns in the same row, when compared across any rows where one number is a multiple of another, e.g. look at the rows for decimal 32, 64, 128). Both these effects that we see are due to the inherent properties of the numbers with those values and their representation in those number bases. Effect 1) - like a car's odometer - is most noticeable in the Bin(ary) column - because of the way all the 1 bits flip to 0 when the number bumps up to a power of 2 - e.g. when binary 111 (decimal 7) changes to binary 1000 (decimal 8 == 2 ** 3), but the effect can be seen in the other columns as well.

The image at the top of the post is from this Wikipedia page:

Numeral system

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

#### 1 comment:

Vasudev Ram said...

One typo - this line:

I named the output BODH-255.pdf (from Binary Decimal Octal Hexadecimal 255)

should be:

I named the output BODH-255.pdf (from Binary Octal Decimal Hexadecimal 255)