6.8.1. Conversion functions

Here are the modules which do the conversions.
Start python section to interscript/encoding/gb2312.py[1 /1 ]
     1: #line 70 "gb2312.ipk"
     2: from array import array
     3: 
     4: firstval = 0xA1
     5: lastval = 0xFE
     6: width = lastval - firstval + 1
     7: gbsize = width * width
     8: 
     9: tou = array('H')
    10: filename = 'interscript/encoding/gb2312.dat'
    11: f = open(filename,'rb')
    12: tou.fromfile(f,gbsize)
    13: f.close()
    14: 
    15: def gb2312_to_unicode(ch):
    16:   hi = ch >> 8
    17:   lo = ch & 0xFF
    18:   if firstval <= hi <= lastval and firstval <= lo <= lastval:
    19:     return tou[(hi-firstval)*width+lo-firstval]
    20:   else:
    21:     return 0xFFFF
    22: 
    23: def gb2312_to_utf8(s):
    24:   u = ''
    25:   i = 0
    26:   n = len(s)
    27:   while 1:
    28:     ch = s[i]
    29:     i = i + 1
    30:     ch = ch << 8 | s[i]
    31:     u = u + utf8(gb2312_to_unicode(ch))
    32:     i = i + 1
    33:     if i==n: break
    34:   return u
    35: 
End python section to interscript/encoding/gb2312.py[1]
Start python section to interscript/encoding/gb2312_80.py[1 /1 ]
     1: #line 105 "gb2312.ipk"
     2: from array import array
     3: 
     4: firstval = 0x21
     5: lastval = 0x7E
     6: width = lastval - firstval + 1
     7: gbsize = width * width
     8: 
     9: tou = array('H')
    10: filename = 'interscript/encoding/gb2312.dat'
    11: f = open(filename,'rb')
    12: tou.fromfile(f,gbsize)
    13: f.close()
    14: del f
    15: 
    16: def gb2312_80_to_unicode(ch):
    17:   hi = ch >> 8
    18:   lo = ch & 0xFF
    19:   if firstval <= hi <= lastval and firstval <= lo <= lastval:
    20:     return tou[(hi-firstval)*width+lo-firstval]
    21:   else:
    22:     return 0xFFFF
    23: 
    24: def gb2312_80_to_utf8(s):
    25:   u = ''
    26:   i = 0
    27:   n = len(s)
    28:   while 1:
    29:     ch = s[i]
    30:     i = i + 1
    31:     ch = ch << 8 | s[i]
    32:     u = u + utf8(gb2312_80_to_unicode(ch))
    33:     i = i + 1
    34:     if i==n: break
    35:   return u
    36: 
End python section to interscript/encoding/gb2312_80.py[1]