Python unicode码转utf8,pythonutf8,Python unico
Python unicode码转utf8,pythonutf8,Python unico
Python unicode码转utf8 配合这篇文章来看 http://www.qianxingzhem.com/post-1499.html
def h2b(x): """十六进制转二进制""" result = [] if x==0: return ['0'] while x != 1: result.append(str(x%2)) x /= 2 result.append('1') result.reverse() if len(result) % 4 != 0: for i in range(0,(4 - len(result) % 4)): result.insert(0,'0') return "".join(result)def c2u(x): """unicode 转 utf8""" result = '' if 0x0 <= x and x <= 0x7F: result = h2b(x) elif 0x80 <= x and x <= 0x7FF: if len(h2b(x)) == 8: tmp = '000' + h2b(x) else: tmp = h2b(x)[1:] result = '110'+ tmp[0:5] + '10' + tmp[5:] elif 0x800 <= x and x <= 0xFFFF: if len(h2b(x)) == 12: tmp = '0000' + h2b(x) else: tmp = h2b(x) result = '1110'+ tmp[0:4] + '10' + tmp[4:10] + '10' + tmp[10:] return b2h(result)def b2h(x): """二进制转十六进制""" hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F'] result = [] def getValue(bin): value = 0 for i in range(4): value += int(bin[i]) * 2**(3-i) return value for i in range(len(x)/4): result.append(hex[getValue(x[i*4:(i+1)*4])]) return result#该片段来自于http://byrx.net
相关内容
- 寻找时间点,时间,任务需要每隔五分钟执行一
- 下载360最新的安装包,下载360安装包,从360官网上下载最
- Python的logging模块封装,pythonlogging模块,使得logging模块用
- 排序,,q = lambda x
- Extended Euclid Algorithm,euclidalgorithm,d = gcd(a, b
- python实现的类文件接口的gopher客户端,,import socke
- python操作sqlite的CRUD,pythonsqlitecrud,import sqlit
- 在centos上安装python pip和easy_install,centoseasy_install,在c
- centos安装 python-dev包提示No package python-dev available,cen
- 初识聚类算法: 基本K均值,初识聚类k均值,基本K均值
评论关闭