Last active
December 21, 2015 06:39
-
-
Save fomightez/6265728 to your computer and use it in GitHub Desktop.
asciihexbin.ipynb from Practical Computing for Biologists by Steven H. D. Haddock and Casey W. Dunn AS A STATIC IPYTHON Notebook. 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 an interactive IPython gist of this at https://www.pyth…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "asciihexbin.ipynb" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"asciihexbin.py" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
">code by Steven H. D. Haddock and Casey W. Dunn as described in: \n", | |
">Practical Computing for Biologists \n", | |
">by Steven H. D. Haddock and Casey W. Dunn \n", | |
">published in 2011 by Sinauer Associates. \n", | |
">ISBN 978-0-87893-391-4 \n", | |
">\n", | |
">[http://www.sinauer.com/practical-computing-for-biologists.html](http://www.sinauer.com/practical-computing-for-biologists.html) \n", | |
">see [practicalcomputing.org](practicalcomputing.org) \n", | |
">\n", | |
">scripts freely available by the original authors at practicalcomputing.org \n", | |
">DIRECT LINK: [http://practicalcomputing.org/files/pcfb_examples.zip](http://practicalcomputing.org/files/pcfb_examples.zip) \n", | |
">####posted as a Gist and IPython Notebook by Wayne (fomightez at GitHub) with full credit and reference to original code authors." | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"asciihexbin.py prints a table of hex, binary, unicode\n", | |
"values for 0 to 256. <br/>\n", | |
"#The first 127 characters are also the ascii character set <br/>\n", | |
"# uses this built-in module to convert to bin and hex <br/>\n", | |
"# This program is described in Appendix 6 (pg. 506) of PCfB <br/>\n", | |
"The code:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def int2bin(n, count=8):\n", | |
" \"\"\"returns the binary of integer n, using count number of digits\"\"\"\n", | |
" return \"\".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])\n", | |
"\n", | |
"ValuesToPrint = 256\n", | |
"for x in range(ValuesToPrint):\n", | |
"\t# split parameters of the % operator across lines\n", | |
"\tprint \"%03d\\t%8s\\t%s\\t%s\" % (\n", | |
"\tx, # the decimal value\n", | |
"\tint2bin(x), # binary representation\n", | |
"\thex(x).upper(), # hexadecimal value\n", | |
"\tunichr(x) # unicode character\n", | |
"\t)\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"000\t00000000\t0X0\t\u0000\n", | |
"001\t00000001\t0X1\t\u0001\n", | |
"002\t00000010\t0X2\t\u0002\n", | |
"003\t00000011\t0X3\t\u0003\n", | |
"004\t00000100\t0X4\t\u0004\n", | |
"005\t00000101\t0X5\t\u0005\n", | |
"006\t00000110\t0X6\t\u0006\n", | |
"007\t00000111\t0X7\t\u0007\n", | |
"008\t00001000\t0X8\t\b\n", | |
"009\t00001001\t0X9\t\t\n", | |
"010\t00001010\t0XA\t\n", | |
"\n", | |
"011\t00001011\t0XB\t\u000b", | |
"\n", | |
"012\t00001100\t0XC\t\f", | |
"\n", | |
"013\t00001101\t0XD\t\r\n", | |
"014\t00001110\t0XE\t\u000e\n", | |
"015\t00001111\t0XF\t\u000f\n", | |
"016\t00010000\t0X10\t\u0010\n", | |
"017\t00010001\t0X11\t\u0011\n", | |
"018\t00010010\t0X12\t\u0012\n", | |
"019\t00010011\t0X13\t\u0013\n", | |
"020\t00010100\t0X14\t\u0014\n", | |
"021\t00010101\t0X15\t\u0015\n", | |
"022\t00010110\t0X16\t\u0016\n", | |
"023\t00010111\t0X17\t\u0017\n", | |
"024\t00011000\t0X18\t\u0018\n", | |
"025\t00011001\t0X19\t\u0019\n", | |
"026\t00011010\t0X1A\t\u001a\n", | |
"027\t00011011\t0X1B\t\u001b\n", | |
"028\t00011100\t0X1C\t\u001c", | |
"\n", | |
"029\t00011101\t0X1D\t\u001d", | |
"\n", | |
"030\t00011110\t0X1E\t\u001e", | |
"\n", | |
"031\t00011111\t0X1F\t\u001f\n", | |
"032\t00100000\t0X20\t \n", | |
"033\t00100001\t0X21\t!\n", | |
"034\t00100010\t0X22\t\"\n", | |
"035\t00100011\t0X23\t#\n", | |
"036\t00100100\t0X24\t$\n", | |
"037\t00100101\t0X25\t%\n", | |
"038\t00100110\t0X26\t&\n", | |
"039\t00100111\t0X27\t'\n", | |
"040\t00101000\t0X28\t(\n", | |
"041\t00101001\t0X29\t)\n", | |
"042\t00101010\t0X2A\t*\n", | |
"043\t00101011\t0X2B\t+\n", | |
"044\t00101100\t0X2C\t,\n", | |
"045\t00101101\t0X2D\t-\n", | |
"046\t00101110\t0X2E\t.\n", | |
"047\t00101111\t0X2F\t/\n", | |
"048\t00110000\t0X30\t0\n", | |
"049\t00110001\t0X31\t1\n", | |
"050\t00110010\t0X32\t2\n", | |
"051\t00110011\t0X33\t3\n", | |
"052\t00110100\t0X34\t4\n", | |
"053\t00110101\t0X35\t5\n", | |
"054\t00110110\t0X36\t6\n", | |
"055\t00110111\t0X37\t7\n", | |
"056\t00111000\t0X38\t8\n", | |
"057\t00111001\t0X39\t9\n", | |
"058\t00111010\t0X3A\t:\n", | |
"059\t00111011\t0X3B\t;\n", | |
"060\t00111100\t0X3C\t<\n", | |
"061\t00111101\t0X3D\t=\n", | |
"062\t00111110\t0X3E\t>\n", | |
"063\t00111111\t0X3F\t?\n", | |
"064\t01000000\t0X40\t@\n", | |
"065\t01000001\t0X41\tA\n", | |
"066\t01000010\t0X42\tB\n", | |
"067\t01000011\t0X43\tC\n", | |
"068\t01000100\t0X44\tD\n", | |
"069\t01000101\t0X45\tE\n", | |
"070\t01000110\t0X46\tF\n", | |
"071\t01000111\t0X47\tG\n", | |
"072\t01001000\t0X48\tH\n", | |
"073\t01001001\t0X49\tI\n", | |
"074\t01001010\t0X4A\tJ\n", | |
"075\t01001011\t0X4B\tK\n", | |
"076\t01001100\t0X4C\tL\n", | |
"077\t01001101\t0X4D\tM\n", | |
"078\t01001110\t0X4E\tN\n", | |
"079\t01001111\t0X4F\tO\n", | |
"080\t01010000\t0X50\tP\n", | |
"081\t01010001\t0X51\tQ\n", | |
"082\t01010010\t0X52\tR\n", | |
"083\t01010011\t0X53\tS\n", | |
"084\t01010100\t0X54\tT\n", | |
"085\t01010101\t0X55\tU\n", | |
"086\t01010110\t0X56\tV\n", | |
"087\t01010111\t0X57\tW\n", | |
"088\t01011000\t0X58\tX\n", | |
"089\t01011001\t0X59\tY\n", | |
"090\t01011010\t0X5A\tZ\n", | |
"091\t01011011\t0X5B\t[\n", | |
"092\t01011100\t0X5C\t\\\n", | |
"093\t01011101\t0X5D\t]\n", | |
"094\t01011110\t0X5E\t^\n", | |
"095\t01011111\t0X5F\t_\n", | |
"096\t01100000\t0X60\t`\n", | |
"097\t01100001\t0X61\ta\n", | |
"098\t01100010\t0X62\tb\n", | |
"099\t01100011\t0X63\tc\n", | |
"100\t01100100\t0X64\td\n", | |
"101\t01100101\t0X65\te\n", | |
"102\t01100110\t0X66\tf\n", | |
"103\t01100111\t0X67\tg\n", | |
"104\t01101000\t0X68\th\n", | |
"105\t01101001\t0X69\ti\n", | |
"106\t01101010\t0X6A\tj\n", | |
"107\t01101011\t0X6B\tk\n", | |
"108\t01101100\t0X6C\tl\n", | |
"109\t01101101\t0X6D\tm\n", | |
"110\t01101110\t0X6E\tn\n", | |
"111\t01101111\t0X6F\to\n", | |
"112\t01110000\t0X70\tp\n", | |
"113\t01110001\t0X71\tq\n", | |
"114\t01110010\t0X72\tr\n", | |
"115\t01110011\t0X73\ts\n", | |
"116\t01110100\t0X74\tt\n", | |
"117\t01110101\t0X75\tu\n", | |
"118\t01110110\t0X76\tv\n", | |
"119\t01110111\t0X77\tw\n", | |
"120\t01111000\t0X78\tx\n", | |
"121\t01111001\t0X79\ty\n", | |
"122\t01111010\t0X7A\tz\n", | |
"123\t01111011\t0X7B\t{\n", | |
"124\t01111100\t0X7C\t|\n", | |
"125\t01111101\t0X7D\t}\n", | |
"126\t01111110\t0X7E\t~\n", | |
"127\t01111111\t0X7F\t\u007f\n", | |
"128\t10000000\t0X80\t\u0080\n", | |
"129\t10000001\t0X81\t\u0081\n", | |
"130\t10000010\t0X82\t\u0082\n", | |
"131\t10000011\t0X83\t\u0083\n", | |
"132\t10000100\t0X84\t\u0084\n", | |
"133\t10000101\t0X85\t\u0085", | |
"\n", | |
"134\t10000110\t0X86\t\u0086\n", | |
"135\t10000111\t0X87\t\u0087\n", | |
"136\t10001000\t0X88\t\u0088\n", | |
"137\t10001001\t0X89\t\u0089\n", | |
"138\t10001010\t0X8A\t\u008a\n", | |
"139\t10001011\t0X8B\t\u008b\n", | |
"140\t10001100\t0X8C\t\u008c\n", | |
"141\t10001101\t0X8D\t\u008d\n", | |
"142\t10001110\t0X8E\t\u008e\n", | |
"143\t10001111\t0X8F\t\u008f\n", | |
"144\t10010000\t0X90\t\u0090\n", | |
"145\t10010001\t0X91\t\u0091\n", | |
"146\t10010010\t0X92\t\u0092\n", | |
"147\t10010011\t0X93\t\u0093\n", | |
"148\t10010100\t0X94\t\u0094\n", | |
"149\t10010101\t0X95\t\u0095\n", | |
"150\t10010110\t0X96\t\u0096\n", | |
"151\t10010111\t0X97\t\u0097\n", | |
"152\t10011000\t0X98\t\u0098\n", | |
"153\t10011001\t0X99\t\u0099\n", | |
"154\t10011010\t0X9A\t\u009a\n", | |
"155\t10011011\t0X9B\t\u009b\n", | |
"156\t10011100\t0X9C\t\u009c\n", | |
"157\t10011101\t0X9D\t\u009d\n", | |
"158\t10011110\t0X9E\t\u009e\n", | |
"159\t10011111\t0X9F\t\u009f\n", | |
"160\t10100000\t0XA0\t\u00a0\n", | |
"161\t10100001\t0XA1\t\u00a1\n", | |
"162\t10100010\t0XA2\t\u00a2\n", | |
"163\t10100011\t0XA3\t\u00a3\n", | |
"164\t10100100\t0XA4\t\u00a4\n", | |
"165\t10100101\t0XA5\t\u00a5\n", | |
"166\t10100110\t0XA6\t\u00a6\n", | |
"167\t10100111\t0XA7\t\u00a7\n", | |
"168\t10101000\t0XA8\t\u00a8\n", | |
"169\t10101001\t0XA9\t\u00a9\n", | |
"170\t10101010\t0XAA\t\u00aa\n", | |
"171\t10101011\t0XAB\t\u00ab\n", | |
"172\t10101100\t0XAC\t\u00ac\n", | |
"173\t10101101\t0XAD\t\u00ad\n", | |
"174\t10101110\t0XAE\t\u00ae\n", | |
"175\t10101111\t0XAF\t\u00af\n", | |
"176\t10110000\t0XB0\t\u00b0\n", | |
"177\t10110001\t0XB1\t\u00b1\n", | |
"178\t10110010\t0XB2\t\u00b2\n", | |
"179\t10110011\t0XB3\t\u00b3\n", | |
"180\t10110100\t0XB4\t\u00b4\n", | |
"181\t10110101\t0XB5\t\u00b5\n", | |
"182\t10110110\t0XB6\t\u00b6\n", | |
"183\t10110111\t0XB7\t\u00b7\n", | |
"184\t10111000\t0XB8\t\u00b8\n", | |
"185\t10111001\t0XB9\t\u00b9\n", | |
"186\t10111010\t0XBA\t\u00ba\n", | |
"187\t10111011\t0XBB\t\u00bb\n", | |
"188\t10111100\t0XBC\t\u00bc\n", | |
"189\t10111101\t0XBD\t\u00bd\n", | |
"190\t10111110\t0XBE\t\u00be\n", | |
"191\t10111111\t0XBF\t\u00bf\n", | |
"192\t11000000\t0XC0\t\u00c0\n", | |
"193\t11000001\t0XC1\t\u00c1\n", | |
"194\t11000010\t0XC2\t\u00c2\n", | |
"195\t11000011\t0XC3\t\u00c3\n", | |
"196\t11000100\t0XC4\t\u00c4\n", | |
"197\t11000101\t0XC5\t\u00c5\n", | |
"198\t11000110\t0XC6\t\u00c6\n", | |
"199\t11000111\t0XC7\t\u00c7\n", | |
"200\t11001000\t0XC8\t\u00c8\n", | |
"201\t11001001\t0XC9\t\u00c9\n", | |
"202\t11001010\t0XCA\t\u00ca\n", | |
"203\t11001011\t0XCB\t\u00cb\n", | |
"204\t11001100\t0XCC\t\u00cc\n", | |
"205\t11001101\t0XCD\t\u00cd\n", | |
"206\t11001110\t0XCE\t\u00ce\n", | |
"207\t11001111\t0XCF\t\u00cf\n", | |
"208\t11010000\t0XD0\t\u00d0\n", | |
"209\t11010001\t0XD1\t\u00d1\n", | |
"210\t11010010\t0XD2\t\u00d2\n", | |
"211\t11010011\t0XD3\t\u00d3\n", | |
"212\t11010100\t0XD4\t\u00d4\n", | |
"213\t11010101\t0XD5\t\u00d5\n", | |
"214\t11010110\t0XD6\t\u00d6\n", | |
"215\t11010111\t0XD7\t\u00d7\n", | |
"216\t11011000\t0XD8\t\u00d8\n", | |
"217\t11011001\t0XD9\t\u00d9\n", | |
"218\t11011010\t0XDA\t\u00da\n", | |
"219\t11011011\t0XDB\t\u00db\n", | |
"220\t11011100\t0XDC\t\u00dc\n", | |
"221\t11011101\t0XDD\t\u00dd\n", | |
"222\t11011110\t0XDE\t\u00de\n", | |
"223\t11011111\t0XDF\t\u00df\n", | |
"224\t11100000\t0XE0\t\u00e0\n", | |
"225\t11100001\t0XE1\t\u00e1\n", | |
"226\t11100010\t0XE2\t\u00e2\n", | |
"227\t11100011\t0XE3\t\u00e3\n", | |
"228\t11100100\t0XE4\t\u00e4\n", | |
"229\t11100101\t0XE5\t\u00e5\n", | |
"230\t11100110\t0XE6\t\u00e6\n", | |
"231\t11100111\t0XE7\t\u00e7\n", | |
"232\t11101000\t0XE8\t\u00e8\n", | |
"233\t11101001\t0XE9\t\u00e9\n", | |
"234\t11101010\t0XEA\t\u00ea\n", | |
"235\t11101011\t0XEB\t\u00eb\n", | |
"236\t11101100\t0XEC\t\u00ec\n", | |
"237\t11101101\t0XED\t\u00ed\n", | |
"238\t11101110\t0XEE\t\u00ee\n", | |
"239\t11101111\t0XEF\t\u00ef\n", | |
"240\t11110000\t0XF0\t\u00f0\n", | |
"241\t11110001\t0XF1\t\u00f1\n", | |
"242\t11110010\t0XF2\t\u00f2\n", | |
"243\t11110011\t0XF3\t\u00f3\n", | |
"244\t11110100\t0XF4\t\u00f4\n", | |
"245\t11110101\t0XF5\t\u00f5\n", | |
"246\t11110110\t0XF6\t\u00f6\n", | |
"247\t11110111\t0XF7\t\u00f7\n", | |
"248\t11111000\t0XF8\t\u00f8\n", | |
"249\t11111001\t0XF9\t\u00f9\n", | |
"250\t11111010\t0XFA\t\u00fa\n", | |
"251\t11111011\t0XFB\t\u00fb\n", | |
"252\t11111100\t0XFC\t\u00fc\n", | |
"253\t11111101\t0XFD\t\u00fd\n", | |
"254\t11111110\t0XFE\t\u00fe\n", | |
"255\t11111111\t0XFF\t\u00ff\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**See the code in action and explore it interactively [here](https://www.pythonanywhere.com/gists/6265565/asciihexbin.py/ipython2/).** " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Obtain a copy of this entire IPython Notebook [here](https://gist.github.com/fomightez/6265728) in order to explore it interactively.** " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
" \n", | |
" \n", | |
" \n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"<br/>\n", | |
"Additional aid and exploration below:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%whos" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Variable Type Data/Info\n", | |
"-------------------------------------\n", | |
"ValuesToPrint int 256\n", | |
"int2bin function <function int2bin at 0x10b5b1230>\n", | |
"x int 255\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The above special command lets us see what is defined and can be used in an IPython Notebook. \n", | |
"(For some reason it doesn't work for any of the initiating variables over in the interactive gist console.)" | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment