<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: decode code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 03:44:42 GMT</pubDate>
    <description>DZone Snippets: decode code</description>
    <item>
      <title>UTF-8 Converter //JavaScript Object</title>
      <link>http://snippets.dzone.com/posts/show/5294</link>
      <description>&lt;br /&gt;&lt;br /&gt;&lt;a href="http://jsfromhell.com/geral/utf-8"&gt;&lt;br /&gt;Converts a sequence of ANSI characters to UTF-8 and vice-versa.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/geral/utf-8 [v1.0]&lt;br /&gt;&lt;br /&gt;UTF8 = {&lt;br /&gt;	encode: function(s){&lt;br /&gt;		for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i &lt; l;&lt;br /&gt;			s[i] = (c = s[i].charCodeAt(0)) &gt;= 127 ? o(0xc0 | (c &gt;&gt;&gt; 6)) + o(0x80 | (c &amp; 0x3f)) : s[i]&lt;br /&gt;		);&lt;br /&gt;		return s.join("");&lt;br /&gt;	},&lt;br /&gt;	decode: function(s){&lt;br /&gt;		for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i &lt; l;&lt;br /&gt;			((a = s[i][c](0)) &amp; 0x80) &amp;&amp;&lt;br /&gt;			(s[i] = (a &amp; 0xfc) == 0xc0 &amp;&amp; ((b = s[i + 1][c](0)) &amp; 0xc0) == 0x80 ?&lt;br /&gt;			o(((a &amp; 0x03) &lt;&lt; 6) + (b &amp; 0x3f)) : o(128), s[++i] = "")&lt;br /&gt;		);&lt;br /&gt;		return s.join("");&lt;br /&gt;	}&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var s = "a&#225;&#233;&#237;&#243;&#250;e";&lt;br /&gt;document.write(&lt;br /&gt;	('UTF8.encode("' + s + '") = ').bold(), UTF8.encode(s), "&lt;br /&gt;",&lt;br /&gt;	('UTF8.decode(UTF8.encode("' + s + '"))) = ').bold(), UTF8.decode(UTF8.encode(s))&lt;br /&gt;);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 30 Mar 2008 16:52:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5294</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>str_hex and hex_str</title>
      <link>http://snippets.dzone.com/posts/show/2039</link>
      <description>// Convert hex to string and vice versa.&lt;br /&gt;//&lt;br /&gt;// (Source: http://codedump.jonasjohn.de/)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function str_hex($string){&lt;br /&gt;    $hex='';&lt;br /&gt;    for ($i=0; $i &lt; strlen($string); $i++){&lt;br /&gt;        $hex .= dechex(ord($string[$i]));&lt;br /&gt;    }&lt;br /&gt;    return $hex;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function hex_str($hex){&lt;br /&gt;    $string='';&lt;br /&gt;    for ($i=0; $i &lt; strlen($hex)-1; $i+=2){&lt;br /&gt;        $string .= chr(hexdec($hex[$i].$hex[$i+1]));&lt;br /&gt;    }&lt;br /&gt;    return $string;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// example:&lt;br /&gt;&lt;br /&gt;$hex = str_hex("test sentence...");&lt;br /&gt;// $hex contains 746573742073656e74656e63652e2e2e&lt;br /&gt;&lt;br /&gt;print hex_str($hex);&lt;br /&gt;// outputs: test sentence...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 01:41:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2039</guid>
      <author>jonasj (Jonas J.)</author>
    </item>
    <item>
      <title>Using base64 to encode/decode data</title>
      <link>http://snippets.dzone.com/posts/show/1618</link>
      <description>If you have binary data, you can encode it with&lt;br /&gt;ascii character to store it more safely.&lt;br /&gt;base64 module is an efficient choice to do so.&lt;br /&gt;It uses characters from this set&lt;br /&gt;('=' is used for padding at the end.)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;br /&gt;abcdefghijklmnopqrstuvwxyz&lt;br /&gt;0123456789+/&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Here's how to use it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; import base64&lt;br /&gt;&gt;&gt;&gt; base64.encodestring('hello world')&lt;br /&gt;'aGVsbG8gd29ybGQ=\n'&lt;br /&gt;&gt;&gt;&gt; base64.decodestring(_)&lt;br /&gt;'hello world'&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;If the text is long, the base64 module will split&lt;br /&gt;the encoded data into multiple lines.&lt;br /&gt;If you don't wan't it to be split. You can use&lt;br /&gt;binascii.b2a_base64 instead of base64.encodestring and&lt;br /&gt;binascii.a2b_base64 instead of base64.decodestring&lt;br /&gt;</description>
      <pubDate>Thu, 02 Mar 2006 19:28:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1618</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Reading a 24-bit icon in an mbm file</title>
      <link>http://snippets.dzone.com/posts/show/1476</link>
      <description>Here's an example how I read the icon and display it on canvas&lt;br /&gt;It's a continued part of my previous &lt;a href=http://discussion.forum.nokia.com/forum/showthread.php?t=63608&gt;mbm hack&lt;/a&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; from appuifw import *&lt;br /&gt;&gt;&gt;&gt; from struct import unpack&lt;br /&gt;&gt;&gt;&gt; def readL(f, pos=None):     # helping function&lt;br /&gt;...     if pos is not None:&lt;br /&gt;...         f.seek(pos)&lt;br /&gt;...     return unpack('L', f.read(4))[0]&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; fxmbm = 'E:\\system\\apps\\FExplorer\\FExplorer.mbm'&lt;br /&gt;&gt;&gt;&gt; f = open(fxmbm, 'rb')&lt;br /&gt;&gt;&gt;&gt; trailer = readL(f, 16)&lt;br /&gt;&gt;&gt;&gt; num = readL(f, trailer)   # 19 icons&lt;br /&gt;&gt;&gt;&gt; offset = []&lt;br /&gt;&gt;&gt;&gt; for i in range(num):&lt;br /&gt;...   offset.append(readL(f))&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; offset&lt;br /&gt;[20L, 68L, 116L, 474L, 588L, 1038L, 1228L, 1636L, 1864L, 2277L, 2430L, 3735L, 41&lt;br /&gt;69L, 4569L, 4697L, 5197L, 5370L, 5897L, 6062L]&lt;br /&gt;&gt;&gt;&gt; start = offset[2]  # folder icon&lt;br /&gt;&gt;&gt;&gt; f.seek(start)&lt;br /&gt;&gt;&gt;&gt; length = readL(f) - readL(f)    # length of data section&lt;br /&gt;&gt;&gt;&gt; width, height = readL(f), readL(f)  # 16 x 13&lt;br /&gt;&gt;&gt;&gt; f.seek(start+0x28)  # start of data section&lt;br /&gt;&gt;&gt;&gt; data_enc = f.read(length)   # got the data&lt;br /&gt;&gt;&gt;&gt; def rle24_decode(bytes):&lt;br /&gt;...     out = []&lt;br /&gt;...     i = 0&lt;br /&gt;...     while i &lt; len(bytes):&lt;br /&gt;...         n = ord(bytes[i])&lt;br /&gt;...         i += 1&lt;br /&gt;...         if n &lt; 0x80:&lt;br /&gt;...             out.append( bytes[i:i+3] * (n+1) )&lt;br /&gt;...             i += 3&lt;br /&gt;...         else:&lt;br /&gt;...             n = 0x100 - n&lt;br /&gt;...             out.append( bytes[i:i+3*n] )&lt;br /&gt;...             i += 3*n&lt;br /&gt;...     return ''.join(out)&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; data = rel24_decode(data_enc)&lt;br /&gt;&gt;&gt;&gt; app.body = canvas = Canvas()&lt;br /&gt;&gt;&gt;&gt; for j in range(height):&lt;br /&gt;...     for i in range(width):&lt;br /&gt;...         p = 3*(j*width+i)&lt;br /&gt;...         color = [ord(data[p+k]) for k in (2,1,0)]  # It's BGR&lt;br /&gt;...         canvas.point((i,j), tuple(color))&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt;  # folder icon is drawn on screen&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 14 Feb 2006 12:27:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1476</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
