Python unicode码转utf8,pythonutf8,[Python]代码de
Python unicode码转utf8,pythonutf8,[Python]代码de
[Python]代码
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
相关内容
- 一个非常高效的提取内容关键词的python代码,提取关键
- python使用连分数计算常数e,python分数常数e,# Calculatin
- python通过ftplib登录到ftp服务器,pythonftplib,import ftpli
- Python和Singleton (单件)模式实现代码,pythonsingleton,我
- 批量修改cisco交换机密码,修改cisco交换机,# -*- coding
- 根据mp3文件的tag重命名mp3文件,mp3文件tag重命名,此脚本
- 点灯游戏及其求解,点灯游戏求解,Python语言: 点灯
- python 多线程,python,求教如何控制并发线程的个
- Search 程序如果寫的不好,希望大家能夠教導我,,[Pyt
- Backup 程序如果寫的不好,希望大家能夠教導我,,[Pyt
评论关闭