Python中unicode码转utf8的方法
Python #编码转换2012-11-25 22:36
代码如下:
def h2b(x):
"""十六进制转二进制 http://yige.org"""
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相关文章
- Python二叉树算法实现 2012/11/25
- Python实现的豆瓣电影信息查询 2012/11/25
- Python实现双倍超立方数 2012/11/25
- 用Python实现定时关机 2012/11/25
- 用Python实现的简易采集爬虫 2012/11/23
- Python实现QQ登陆 2012/11/23
- 用Python按日期导出所有微信 2012/11/23
- Python使用global语句 2012/11/23
- 说说Python的版本选择、IDE、库 2012/11/23
- Python实现的记事本尾巴(仿QQ尾巴) 2012/11/23