Top-rated recipes tagged "binary"http://code.activestate.com/recipes/tags/binary/top/2017-03-12T23:10:48-07:00ActiveState Code RecipesSorting big files the Python 2.6 way (Python)
2009-05-30T21:51:09-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/
<p style="color: grey">
Python
recipe 576755
by <a href="/recipes/users/924636/">Gabriel Genellina</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>This is just a rewrite of <a href="http://code.activestate.com/recipes/466302/">Recipe 466302</a> "Sorting big files the Python 2.4 way", taking advantage of heapq.merge, context managers, and other niceties of newer Python versions. It can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p>
Generate a PDF cheat sheet for converting 0 to 255 between bin / oct / dec / hex (Python)
2016-10-10T20:40:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580706-generate-a-pdf-cheat-sheet-for-converting-0-to-255/
<p style="color: grey">
Python
recipe 580706
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/octal/">octal</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to generate a PDF cheat sheet, that contains a table for conversion of the numbers 0 to 255 (the numbers that can fit in one byte) between binary, octal, decimal and hexadecimal representations. The table has four columns, one for each of those bases, and 256 rows, for the numbers 0 to 255.</p>
<p>TO use the table, you can look for a number, say in decimal, in the Dec(imal) column (or use the search function of your PDF viewer), then when you find it in some row, just look at the other 3 columns in that row, to find the value of that number in binary, octal and hexadecimal. And use the same procedure if starting with a number in any of the other three bases.</p>
File read/write routines (Python)
2010-04-18T17:18:50-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577199-file-readwrite-routines/
<p style="color: grey">
Python
recipe 577199
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/read/">read</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/write/">write</a>).
Revision 2.
</p>
<p>For small dirty hacks in Perl has a module File::Slurp.
I wrote two simple functions when I moving from Perl to Python - one for reading files and second for writing files. Valuable data is list of lines or blob (additionally specified argument binmode=1).</p>
Set product (Python)
2010-04-12T19:06:22-07:00Paul Ghttp://code.activestate.com/recipes/users/4173653/http://code.activestate.com/recipes/577190-set-product/
<p style="color: grey">
Python
recipe 577190
by <a href="/recipes/users/4173653/">Paul G</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/multiply/">multiply</a>, <a href="/recipes/tags/product/">product</a>, <a href="/recipes/tags/set/">set</a>).
</p>
<p>This recipe provides a subclass of set which implements __mul__ and __pow__ to support the computation of set products. When multiplying two sets together, a new set is produced with elements which are the inter-set pairs.</p>
Number of bits needed to store an integer, and its binary representation (Python)
2017-03-12T23:10:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580762-number-of-bits-needed-to-store-an-integer-and-its-/
<p style="color: grey">
Python
recipe 580762
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/integers/">integers</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/representation/">representation</a>, <a href="/recipes/tags/type/">type</a>).
</p>
<p>This recipe shows how to find, via Python code, the number of bits needed to store an integer, and how to generate its binary representation. It does this for integers from 0 to 256.</p>
<p>More details and full output here:</p>
<p><a href="https://jugad2.blogspot.in/2017/03/find-number-of-bits-needed-to-store.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/find-number-of-bits-needed-to-store.html</a></p>
A binary file split utility in Python (Python)
2016-04-07T18:19:35-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580637-a-binary-file-split-utility-in-python/
<p style="color: grey">
Python
recipe 580637
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple binary file split utility in Python.</p>
<p>It takes two command line arguments: 1) the name of the input file to split , 2) the number of bytes per file into which to split the input.</p>
A Building Block, Bash Binary File Manipulation... (Bash)
2013-01-29T22:07:57-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578441-a-building-block-bash-binary-file-manipulation/
<p style="color: grey">
Bash
recipe 578441
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 2.
</p>
<p>Apologies for any typos, and IF this has been done before...</p>
<p>The code generates a 256 byte binary file of _characters_ 0x00 to 0xFF for general usage and generates another binary file manipulated in a basic way.</p>
<p>The for loops in the code are purely for DEMO purposes only.</p>
<p>This is Public Domain and you may do with it as you please. I have uploaded it elsewhere too...</p>
<p>Watch for wordwrapping, etc and read the code for more information...</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
A Shell, Binary To Hexadecimal To Decimal Demo... (Bash)
2013-01-11T18:31:03-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578413-a-shell-binary-to-hexadecimal-to-decimal-demo/
<p style="color: grey">
Bash
recipe 578413
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/terminal/">terminal</a>).
</p>
<p>This little DEMO may be common knowledge to the big guns but not to amateurs like myself, so......</p>
<p>This is an Apple Macbbok Shell/Terminal DEMO shell script to show how to:-</p>
<p>1) Create a binary file...
2) Save it to your DEFAULT /directory/drwawer/folder/...
3) Display a hexadecimal dump of said binary file to prove that it is binary...
4) Select a single BYTE of that file and save it as an ASCII text decimal _number_, also to your DEFAULT /directory/drawer/folder/...
5) Read this ASCII text decimal number back in again...
6) Add this string representation to a number...
7) Stop...</p>
<p>It was intended purely for OSX 10.7.5 and above using the default terminal and shell...</p>
<p>It does work on many Linux flavours and shells/terminals also however.</p>
<p>Written so the anyone can understand what is going on.</p>
<p>The two files generated and saved in this DEMO to your DEFAULT /directory/drawer/folder/ are:-</p>
<p>BinaryString.dat
BinaryString.txt</p>
<p>This WILL lead to something very unusual in the not too distant future...</p>
<p>This is Public Domain and you may do with it as you wish...</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
<p>Bazza, G0LCU...</p>
String to Binary (Python)
2014-07-30T07:20:22-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578291-string-to-binary/
<p style="color: grey">
Python
recipe 578291
by <a href="/recipes/users/4183895/">p@ntut$</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/to/">to</a>).
Revision 2.
</p>
<p>String to binary snippet, python3+</p>
Random Binary List (Python)
2011-11-09T19:38:14-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577944-random-binary-list/
<p style="color: grey">
Python
recipe 577944
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_binary_list/">random_binary_list</a>).
</p>
<p>This recipe returns a list of size n such that the contents of the returned list are random 0s and 1s. It returns a random binary list of size n.</p>
Perpetual calendar (Python)
2011-04-15T20:44:54-07:00sundanceriddlehttp://code.activestate.com/recipes/users/4177681/http://code.activestate.com/recipes/577657-perpetual-calendar/
<p style="color: grey">
Python
recipe 577657
by <a href="/recipes/users/4177681/">sundanceriddle</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/calendar/">calendar</a>).
</p>
<p>Represents the so-called "perpetual clock" ( <a href="http://www.evilmadscientist.com/article.php/perpetualcalendar" rel="nofollow">http://www.evilmadscientist.com/article.php/perpetualcalendar</a> ) with numbers instead of pennies. Thinking about making a simple window with heads/tails images.</p>
Decoding Binary Files (Python)
2011-03-15T00:11:10-07:00Yony Kochinskihttp://code.activestate.com/recipes/users/4175703/http://code.activestate.com/recipes/577610-decoding-binary-files/
<p style="color: grey">
Python
recipe 577610
by <a href="/recipes/users/4175703/">Yony Kochinski</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decode/">decode</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>One way to read files that contain binary fields is to use the <code>struct</code> module. However, to do this properly one must learn struct's format characters, which may look especially cryptic when sprinkled around the code. So instead, I use a wrapper object that presents a simple interface as well as type names that are more inline with many <a href="http://en.wikipedia.org/wiki/Interface_description_language">IDLs</a>.</p>
Binary search function. (Python)
2011-02-07T06:37:52-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577565-binary-search-function/
<p style="color: grey">
Python
recipe 577565
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/binary_search/">binary_search</a>, <a href="/recipes/tags/bsearch/">bsearch</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lower_bound/">lower_bound</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/upper_bound/">upper_bound</a>).
Revision 3.
</p>
<p>For a number of years Python has provided developers with the special parameters 'cmp' and 'key' on list.sort and __builtin__.sorted. However Python does not provide a built-in mechanism for doing binary searches on such sorted lists. This recipe provides a simple function that allows you to perform binary searches on your sorted sequences.</p>
Decimal and binary prefix Python snippet (Python)
2010-05-06T17:09:44-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577220-decimal-and-binary-prefix-python-snippet/
<p style="color: grey">
Python
recipe 577220
by <a href="/recipes/users/4167472/">Romain Dartigues</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/prefix/">prefix</a>).
</p>
<p>Really short snipped of a decimal and binary prefix calculator in Python.</p>
<p>Without covering the entire International System of Units (SI) range, it does fit my daily needs. <br />
The code could be <em>way</em> smarter, but i wanted to keep the recipe as basic as i could.</p>
<p>Tested on <code>Python 2.4.1</code> and <code>2.6.4;</code> could work on lower as well.</p>
Nicer struct syntax thanks to Py3 metaclasses (Python)
2009-02-25T07:39:52-08:00Daniel Brodiehttp://code.activestate.com/recipes/users/1892511/http://code.activestate.com/recipes/576666-nicer-struct-syntax-thanks-to-py3-metaclasses/
<p style="color: grey">
Python
recipe 576666
by <a href="/recipes/users/1892511/">Daniel Brodie</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/py3/">py3</a>, <a href="/recipes/tags/struct/">struct</a>).
</p>
<p>This is a quick-hack module I wrote up in a couple of hours that allows for a nicer syntax to build up struct-like binary packing and unpacking. The point was to get it to be concise and as C-like as possible. This script requires python3 for it's improved metaclass support.</p>
Converting arbitrary size Python integers to packed binary data strings (Python)
2009-01-16T13:27:29-08:00David Mosshttp://code.activestate.com/recipes/users/4124829/http://code.activestate.com/recipes/576617-converting-arbitrary-size-python-integers-to-packe/
<p style="color: grey">
Python
recipe 576617
by <a href="/recipes/users/4124829/">David Moss</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/integers/">integers</a>, <a href="/recipes/tags/pack/">pack</a>, <a href="/recipes/tags/struct/">struct</a>, <a href="/recipes/tags/unpack/">unpack</a>, <a href="/recipes/tags/unsigned/">unsigned</a>).
</p>
<p>Routines for converting <strong>large</strong> unsigned arbitrary sized Python integers to packed binary data strings and vice versa.</p>
Convert from decimal to base-n for any positive n less than or equal to 10 (Python)
2013-08-09T05:56:16-07:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/578630-convert-from-decimal-to-base-n-for-any-positive-n-/
<p style="color: grey">
Python
recipe 578630
by <a href="/recipes/users/4187478/">Samuel James Erickson</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/octal/">octal</a>).
</p>
<p>Takes in any non-negative integer and converts to the desired base-n for 1<=n<=10.</p>
Converty decimal/integer number to binary string in Python (Python)
2009-07-17T09:17:32-07:00Vishal Saprehttp://code.activestate.com/recipes/users/4166412/http://code.activestate.com/recipes/576847-converty-decimalinteger-number-to-binary-string-in/
<p style="color: grey">
Python
recipe 576847
by <a href="/recipes/users/4166412/">Vishal Sapre</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>This is a very simple look-up based method for converting integer to binary string. In my tests, using 'timeit', its the fastest around. And with increasing value of input decimal number, it surpases any 'run time computation' based method doing the above.</p>