<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eyedbits &#187; python</title>
	<atom:link href="http://www.eyedbits.com/?cat=13&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.eyedbits.com</link>
	<description>links, tutorials and code fragments regarding computer graphics</description>
	<lastBuildDate>Sat, 23 Jun 2018 05:45:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>utility string functions</title>
		<link>http://www.eyedbits.com/?p=164</link>
		<comments>http://www.eyedbits.com/?p=164#comments</comments>
		<pubDate>Thu, 22 Nov 2012 10:52:35 +0000</pubDate>
		<dc:creator><![CDATA[busk]]></dc:creator>
				<category><![CDATA[general coding]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.eyedbits.com/?p=164</guid>
		<description><![CDATA[convert string into binary representation (8 bits per char) split string into chars, for each char get ordinal number, and create string using 8bit  format of the number. Concat all def str_2_binrep(s): return "".join(['{0:008b}'.format(ord(c)) for c in list(s)]) example: &#62;&#62;&#62; &#8230; <a href="http://www.eyedbits.com/?p=164">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<h1>convert string into binary representation (8 bits per char)</h1>
<p>split string into chars, for each char get ordinal number, and create string using 8bit  format of the number. Concat all</p>
<pre>def str_2_binrep(s):
  return "".join(['{0:008b}'.format(ord(c)) for c in list(s)])

example:

&gt;&gt;&gt; str_2_binrep("Hello World")
'0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100'</pre>
<p>If you want to have a space in between, just write</p>
<pre>'{0:008b} '</pre>
<p>instead.</p>
<h2>split string into groups of n characters</h2>
<p>fill array with substrings, range(start, stop, stepwidth)</p>
<pre>def str_splitintogroups(s, n):
  return [s[i : (i+n) ] for i in range(0, len(s), n)]

example:

&gt;&gt;&gt; str_splitintogroups("split string into groups", 3)
['spl', 'it ', 'str', 'ing', ' in', 'to ', 'gro', 'ups']</pre>
<h2>XOR of two strings</h2>
<p>zip builds tuples containing the ith char of each string; for each pair the xor of the octal number of the char is converted into char and appended</p>
<pre>def str_xor(s1, s2):
  return "".join([chr( ord(c1) ^ ord(c2) ) for (c1,c2) in zip(s1, s2)])

example:

&gt;&gt;&gt; str_xor("Hello", "World")
'\x1f\n\x1e\x00\x0b'

&gt;&gt;&gt; str_xor("Hello", "Hello")
'\x00\x00\x00\x00\x00'</pre>
<h2>Encode String as hex, and decode back</h2>
<p>String encode, see <a title="http://docs.python.org/2/library/codecs.html#standard-encodings" href="http://docs.python.org/2/library/codecs.html#standard-encodings" target="_blank">http://docs.python.org/2/library/codecs.html#standard-encodings</a> for possible encodings (python 2); output is using 2 digits each per byte of input</p>
<pre>example:

&gt;&gt;&gt; "BibiBlocksberg".encode('hex')
'42696269426c6f636b7362657267'

&gt;&gt;&gt; "BibiBlocksberg".encode('hex').decode('hex')
'BibiBlocksberg'</pre>
<h2>Dec to Hex, Hex to Dec</h2>
<p>How to convert a decimal number into a hex representation:</p>
<pre>examples:

&gt;&gt;&gt; hex(1000)
'0x3e8'

&gt;&gt;&gt; "%X"%1000
'3E8'</pre>
<p>How to convert a from hex (or other) back to decimal representation:</p>
<pre>examples:

&gt;&gt;&gt; int("3E8", 16)
1000

&gt;&gt;&gt; int("1001", 2)
9</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eyedbits.com/?feed=rss2&#038;p=164</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
