<?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; Snippets</title>
	<atom:link href="http://www.eyedbits.com/?cat=8&#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>
		<item>
		<title>How to receive and parse a JSON array</title>
		<link>http://www.eyedbits.com/?p=145</link>
		<comments>http://www.eyedbits.com/?p=145#comments</comments>
		<pubDate>Wed, 18 Jul 2012 14:38:20 +0000</pubDate>
		<dc:creator><![CDATA[busk]]></dc:creator>
				<category><![CDATA[general coding]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.eyedbits.com/?p=145</guid>
		<description><![CDATA[Situation: You got some (client) application, that needs data from a mysql database. The application cannot connect to the database itself. It sends a HTTP GET/POST request to a webserver, that gets the data out of the mysql database and &#8230; <a href="http://www.eyedbits.com/?p=145">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><strong>Situation:</strong><br />
You got some (client) application, that needs data from a mysql database.<br />
The application cannot connect to the database itself. It sends a HTTP GET/POST request to a webserver, that gets the data out of the mysql database and packs it into a JSON Array. This JSON Array is sent back to the client.</p>
<p>The client receives the JSON Array and parses it.</p>
<p>This is the php code for the webserver (inspired from <a href="http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html" title="Basic 4 Android Forum" target="_blank"></a>)</p>
<div class="smallSourceCode">
<pre class="brush: java; title: ; notranslate">	
/**
 * download list of registered users from server at cmd.eyedbits.com
 * 
 * @return list of users
 */
static Vector&lt;String&gt; downloadUserList() {
  Log.i(TAG, String.format(&quot;Downloading UserList&quot;));
  
  String serverUrl = SERVER_URL + SERVER_URL_QUERYUSERLIST;

  //TODO: implement multiple tries with exponential backoff if server is not responding		
  DefaultHttpClient hc = new DefaultHttpClient();
  ResponseHandler &lt;String&gt; res = new BasicResponseHandler();
  HttpGet getMethod = new HttpGet(serverUrl);

  Vector&lt;String&gt; retVector = new Vector&lt;String&gt;(); 
  try {
    String response = hc.execute(getMethod, res);
    Log.i(TAG, &quot;Http response: &quot; + response);
    // todo check if successfull and not error
    
    // parse string
    try {
      JSONArray jsonArray = new JSONArray(response);
      for (int i = 0; i &lt; jsonArray.length(); ++i) {
        JSONObject row = jsonArray.getJSONObject(i);
        String userName = row.getString(&quot;UserName&quot;);
        retVector.add(userName);
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
  } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  
  return retVector;
}
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.eyedbits.com/?feed=rss2&#038;p=145</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to expose data from a mysql database via php/JSON for access via HTTP</title>
		<link>http://www.eyedbits.com/?p=134</link>
		<comments>http://www.eyedbits.com/?p=134#comments</comments>
		<pubDate>Wed, 18 Jul 2012 13:46:22 +0000</pubDate>
		<dc:creator><![CDATA[busk]]></dc:creator>
				<category><![CDATA[general coding]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.eyedbits.com/?p=134</guid>
		<description><![CDATA[Situation: You got some (client) application, that needs data from a mysql database. The application cannot connect to the database itself. It sends a HTTP GET/POST request to a webserver, that gets the data out of the mysql database and &#8230; <a href="http://www.eyedbits.com/?p=134">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><strong>Situation:</strong><br />
You got some (client) application, that needs data from a mysql database.<br />
The application cannot connect to the database itself. It sends a HTTP GET/POST request to a webserver, that gets the data out of the mysql database and packs it into a JSON Array. This JSON Array is sent back to the client.</p>
<p><em>(Follow up situation, the client has to request and parse the data)</em></p>
<p><strong>What you need:</strong></p>
<p>&#8211; mysql database<br />
&#8211; webserver with php</p>
<p>This is the php code for the webserver (inspired from <a href="http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html" title="Basic 4 Android Forum" target="_blank"></a>)</p>
<div class="smallSourceCode">
<pre class="brush: php; title: ; notranslate">&lt;?php
  // connect to Database
  // database specifications, adapt your configuration
  $dbUri = &quot;...&quot;;
  $dbUser = &quot;...&quot;;
  $dbPassword = &quot;...&quot;;
  $dbDatabaseName = &quot;...&quot;;

  // establish connection
  $db = mysql_connect($dbUri, $dbUser, $dbPassword) OR
        die(mysql_error());

  mysql_select_db($dbDatabaseName, $db) OR
        die(mysql_error());

  // example query, adapt to your needs
  $sqlquery = &quot;SELECT ... FROM ... WHERE ... ORDER BY ... ASC;&quot;;
  
  $result = mysql_query($sqlquery) OR
        die(mysql_error());
  
  if (mysql_errno()) {
    header(&quot;HTTP/1.1 500 Internal Server Error&quot;);
  } else {
    $rows = array();
    // fill array
    while($r = mysql_fetch_assoc($result)) {
        $rows[] = $r;
    }
    // encode array as JSON and print it
    print json_encode($rows);
  }
?&gt;</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.eyedbits.com/?feed=rss2&#038;p=134</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
