Popular recipes tagged "bytes"http://code.activestate.com/recipes/tags/bytes/2013-11-03T18:35:18-08:00ActiveState Code RecipesBytes-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> Determine 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>&gt;&gt;&gt; exec(open('/home/G0LCU/Desktop/Code/d2b.py').read()) &gt;&gt;&gt; a=78 &gt;&gt;&gt; type(a) &lt;class 'int'&gt; &gt;&gt;&gt; b=d2b(a) &gt;&gt;&gt; print(b) b'N' &gt;&gt;&gt; type(b) &lt;class 'bytes'&gt; &gt;&gt;&gt; text="\x00(C)2012, B.Walker, G0LCU.\xFF" &gt;&gt;&gt; len(text) 27 &gt;&gt;&gt; type(text) &lt;class 'str'&gt; &gt;&gt;&gt; newtext=t2b(text) &gt;&gt;&gt; len(newtext) 27 &gt;&gt;&gt; print(newtext) b'\x00(C)2012, B.Walker, G0LCU.\xff' &gt;&gt;&gt; type(newtext) &lt;class 'bytes'&gt; </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>&gt;&gt;&gt; import d2b </code></pre> <p>And when imported called as:-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; d2b.d2b(some_number, optional_some_other_mumber)&lt;RETURN/ENTER&gt; </code></pre> <p>OR</p> <pre class="prettyprint"><code>&gt;&gt;&gt; d2b.t2b(some_ASCII_string)&lt;RETURN/ENTER&gt; </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> 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>