python 字符串转字节串

def encode(s):
	result = ''
	index = 1
	for c in s:
		v = hex(ord(c)).replace('0x', '')
		if len(v) == 1:
			v = '0' + v
		result += v
		if index % 32 == 0:
			result += '\n'
		elif index % 4 == 0:
			result += ' '
		index += 1
	print result
	return result
def decode(s):
	s = s.replace(' ', '')
	s = s.replace('\n', '')
	result = ''
	index = 1
	cc = ''
	for c in s:
		cc += c
		if index % 2 == 0:
			result += chr(int(cc, 16))
			cc = ''
		index += 1
	print result
	return result


发布了154 篇原创文章 · 获赞 22 · 访问量 74万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览