Xintong Li

Snippets

Double/Single Byte Character

Python3

'''
Double Byte Character to Single Byte Character
'''

def DBC2SBC(ustring):
     rstring = ''
     for uchar in ustring:
         inside_code = ord(uchar)
         if inside_code == 0x3000:
             inside_code = 0x0020
         else:
             inside_code -= 0xfee0
             if not (0x0021 <= inside_code and inside_code <= 0x7e):
                 rstring += uchar
                 continue
         rstring += chr(inside_code)
     return rstring

'''
Single Byte Character to Double Byte Character
'''
def SBC2DBC(ustring):
     rstring = ''
     for uchar in ustring:
         inside_code = ord(uchar)
         if inside_code == 0x0020:
             inside_code = 0x3000
         else:
             if not (0x0021 <= inside_code and inside_code <= 0x7e):
                 rstring += uchar
                 continue
             inside_code += 0xfee0
         rstring += chr(inside_code)
     return rstring