Latest recipes tagged "bytes"http://code.activestate.com/recipes/tags/bytes/new/2013-11-03T18:35:18-08:00ActiveState Code RecipesDetermine bytes needed to hold integer (Python3) (Python)
2013-11-03T18:35:18-08:00teddy_khttp://code.activestate.com/recipes/users/4187115/http://code.activestate.com/recipes/578766-determine-bytes-needed-to-hold-integer-python3/
<p style="color: grey">
Python
recipe 578766
by <a href="/recipes/users/4187115/">teddy_k</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/integer/">integer</a>).
</p>
<p>Convenience function to determine the number of bytes needed for a specified integer (split into multiple lines for clarity's sake ... could just as easily be a one-liner).</p>
<p>If this is already a defined function within Python, please let me know--a quick web search turned up nothing.</p>
Decimal Number To Byte(s) And String To Byte(s) Converter. (Python)
2012-01-24T21:11:47-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578025-decimal-number-to-bytes-and-string-to-bytes-conver/
<p style="color: grey">
Python
recipe 578025
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>A function to convert decimal integer numbers, (from 0 to 255), into byte(s) format.
Another function calling the above function to convert ASCII strings into byte(s) format.</p>
<p>Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.</p>
<pre class="prettyprint"><code>>>> exec(open('/home/G0LCU/Desktop/Code/d2b.py').read())
>>> a=78
>>> type(a)
<class 'int'>
>>> b=d2b(a)
>>> print(b)
b'N'
>>> type(b)
<class 'bytes'>
>>> text="\x00(C)2012, B.Walker, G0LCU.\xFF"
>>> len(text)
27
>>> type(text)
<class 'str'>
>>> newtext=t2b(text)
>>> len(newtext)
27
>>> print(newtext)
b'\x00(C)2012, B.Walker, G0LCU.\xff'
>>> type(newtext)
<class 'bytes'>
</code></pre>
<p>It requires NOTHING special at all to work and can be run like above or imported from
the correct "Lib" drawer/folder/directorfy as:-</p>
<pre class="prettyprint"><code>>>> import d2b
</code></pre>
<p>And when imported called as:-</p>
<pre class="prettyprint"><code>>>> d2b.d2b(some_number, optional_some_other_mumber)<RETURN/ENTER>
</code></pre>
<p>OR</p>
<pre class="prettyprint"><code>>>> d2b.t2b(some_ASCII_string)<RETURN/ENTER>
</code></pre>
<p>Read the code for much more information...</p>
<p>Issued under the GPL2 licence.</p>
<p>Enjoy finding simple solutions to often very difficult problems.</p>
<p>Bazza, G0LCU.</p>
Bytes-to-human / human-to-bytes converter (Python)
2012-02-02T16:09:52-08:00Giampaolo RodolĂ http://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578019-bytes-to-human-human-to-bytes-converter/
<p style="color: grey">
Python
recipe 578019
by <a href="/recipes/users/4178764/">Giampaolo RodolĂ </a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/humanize/">humanize</a>, <a href="/recipes/tags/string/">string</a>).
Revision 15.
</p>
<p>Here goes.</p>
Humanized representation of a number of bytes (Python)
2010-03-05T17:18:13-08:00Doug Latornellhttp://code.activestate.com/recipes/users/4173209/http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/
<p style="color: grey">
Python
recipe 577081
by <a href="/recipes/users/4173209/">Doug Latornell</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/humanize/">humanize</a>).
Revision 2.
</p>
<p>Convert an integer number of bytes to a string representation. Example: 1024 -> 1 kB</p>
<p>Based quite heavily on <a href="http://mail.python.org/pipermail/python-list/2008-August/1171178.html">http://mail.python.org/pipermail/python-list/2008-August/1171178.html</a></p>
Bitset (Python)
2009-05-04T16:37:02-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576738-bitset/
<p style="color: grey">
Python
recipe 576738
by <a href="/recipes/users/4170000/">geremy condra</a>
(<a href="/recipes/tags/bits/">bits</a>, <a href="/recipes/tags/bytes/">bytes</a>).
</p>
<p>This is a simple Bitset type for Python. It implements the Sequence interface plus __setitem__, the set operations, and string and integer conversions.</p>