Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active December 21, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/6265565 to your computer and use it in GitHub Desktop.
Save fomightez/6265565 to your computer and use it in GitHub Desktop.
asciihexbin.py from Practical Computing for Biologists by Steven H. D. Haddock and Casey W. Dunn. Posted as a Gist by Wayne Decatur (fomightez) with full credit and reference to the original authors and note where the freely share the code online. You can see a static IPython Notebook version at http://nbviewer.ipython.org/6265728
# code by Steven H. D. Haddock and Casey W. Dunn as described in:
# Practical Computing for Biologists
# Steven H. D. Haddock and Casey W. Dunn
# Published in 2011 by Sinauer Associates.
# ISBN 978-0-87893-391-4
# http://www.sinauer.com/practical-computing-for-biologists.html
# see practicalcomputing.org
#
#scripts freely available by the original authors at practicalcomputing.org
#DIRECT LINK: http://practicalcomputing.org/files/pcfb_examples.zip
#
#!/usr/bin/env python
# encoding: utf-8
# print a table of hex, binary, unicode values for 0 to 256
# the first 127 characters are also the ascii character set
# uses this built-in module to convert to bin and hex
def int2bin(n, count=8):
"""returns the binary of integer n, using count number of digits"""
return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
ValuesToPrint = 256
for x in range(ValuesToPrint):
# split parameters of the % operator across lines
print "%03d\t%8s\t%s\t%s" % (
x, # the decimal value
int2bin(x), # binary representation
hex(x).upper(), # hexadecimal value
unichr(x) # unicode character
)
#--------------------------------------------------------#
#****ALL BELOW ADDED BY WAYNE DECATUR TO HELP *********#
#****WITH UNDERSTANDING THIS SCRIPT AND EXPLORING******#
#****FURTHER USING THE INTERACTIVE CONSOLE. ********#
#--------------------------------------------------------#
print "\n\n\n\n---------------------------------------------------------------------------"
print "Following added by Wayne Decatur beyond Haddock and Dunn code, as a starting point"
print "for understanding the script in this gist."
print "---------------------------------------------------------------------------"
import pprint #this is simply to allow use of the pprint command mentioned below
print "\n\nSee the raw code by clicking the blue numbers after 'Gist ID and link' in the side panel."
print "Tip: Opening the code in a separate browswer window can make it easier to follow along."
print "See a static IPython Notebook of the input and output at http://nbviewer.ipython.org/6265728"
print"\nHelp getting started exploring the code in the iPython interactive shell:"
print " You can get a list of the variable and values by typing 'pprint.pprint(locals())' at the prompt."
print " Type the name of a variable or object followed by a '?' to get information."
print "In all cases you simply what is between the single quotes, not including the single quotes."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment