Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

UTF-8 Converter //JavaScript Object




Converts a sequence of ANSI characters to UTF-8 and vice-versa.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/utf-8 [v1.0]

UTF8 = {
	encode: function(s){
		for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
			s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
		);
		return s.join("");
	},
	decode: function(s){
		for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
			((a = s[i][c](0)) & 0x80) &&
			(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
			o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
		);
		return s.join("");
	}
};


Example

var s = "aáéíóúe";
document.write(
	('UTF8.encode("' + s + '") = ').bold(), UTF8.encode(s), "<br />",
	('UTF8.decode(UTF8.encode("' + s + '"))) = ').bold(), UTF8.decode(UTF8.encode(s))
);

str_hex and hex_str

// Convert hex to string and vice versa.
//
// (Source: http://codedump.jonasjohn.de/)

function str_hex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}


function hex_str($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

// example:

$hex = str_hex("test sentence...");
// $hex contains 746573742073656e74656e63652e2e2e

print hex_str($hex);
// outputs: test sentence...

Using base64 to encode/decode data

If you have binary data, you can encode it with
ascii character to store it more safely.
base64 module is an efficient choice to do so.
It uses characters from this set
('=' is used for padding at the end.)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789+/

Here's how to use it.
>>> import base64
>>> base64.encodestring('hello world')
'aGVsbG8gd29ybGQ=\n'
>>> base64.decodestring(_)
'hello world'
>>>

If the text is long, the base64 module will split
the encoded data into multiple lines.
If you don't wan't it to be split. You can use
binascii.b2a_base64 instead of base64.encodestring and
binascii.a2b_base64 instead of base64.decodestring

Reading a 24-bit icon in an mbm file

Here's an example how I read the icon and display it on canvas
It's a continued part of my previous mbm hack.
>>> from appuifw import *
>>> from struct import unpack
>>> def readL(f, pos=None):     # helping function
...     if pos is not None:
...         f.seek(pos)
...     return unpack('L', f.read(4))[0]
...
>>> fxmbm = 'E:\\system\\apps\\FExplorer\\FExplorer.mbm'
>>> f = open(fxmbm, 'rb')
>>> trailer = readL(f, 16)
>>> num = readL(f, trailer)   # 19 icons
>>> offset = []
>>> for i in range(num):
...   offset.append(readL(f))
...
>>> offset
[20L, 68L, 116L, 474L, 588L, 1038L, 1228L, 1636L, 1864L, 2277L, 2430L, 3735L, 41
69L, 4569L, 4697L, 5197L, 5370L, 5897L, 6062L]
>>> start = offset[2]  # folder icon
>>> f.seek(start)
>>> length = readL(f) - readL(f)    # length of data section
>>> width, height = readL(f), readL(f)  # 16 x 13
>>> f.seek(start+0x28)  # start of data section
>>> data_enc = f.read(length)   # got the data
>>> def rle24_decode(bytes):
...     out = []
...     i = 0
...     while i < len(bytes):
...         n = ord(bytes[i])
...         i += 1
...         if n < 0x80:
...             out.append( bytes[i:i+3] * (n+1) )
...             i += 3
...         else:
...             n = 0x100 - n
...             out.append( bytes[i:i+3*n] )
...             i += 3*n
...     return ''.join(out)
...
>>> data = rel24_decode(data_enc)
>>> app.body = canvas = Canvas()
>>> for j in range(height):
...     for i in range(width):
...         p = 3*(j*width+i)
...         color = [ord(data[p+k]) for k in (2,1,0)]  # It's BGR
...         canvas.point((i,j), tuple(color))
...
>>>  # folder icon is drawn on screen
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS