python3unicode转中文_python3 二进制转中文

关于二进制转中文的问题。

>>> name = '你好'

>>> name

'你好'

>>> bytes(name,'unicode_escape')

b'\\u4f60\\u597d'

现在我有一段二进制数据,想要转化成中文。

bytes_name = b'\x60\x4f\x7d\x59'

我尝试使用 bytes_name.decode('unicode_escape')去转化,得到结果'O}Y'`,

我应该怎样做才可以得到正确的结果你好。

谢谢!

===========================================================================

谢谢大家,我已找到答案。

那个二进制数据bytes_name = b'\x60\x4f\x7d\x59'是用c语言写的另一个程序传送过来的数据。

根据大家的提示,我将b'\x60\x4f\x7d\x59'转化成b'\\u4f60\\u597d',然后再decode('unicode_escape')即可。

def parse_unicodestring(unicode_bytes,length):

result_bytes = b''

for i in range(0,length,2):

a = hex(unicode_bytes[i])[2:].encode('unicode_escape') if len(hex(unicode_bytes[i])) == 4 else ('0'+hex(unicode_bytes[i])[2:]).encode('unicode_escape')

b = hex(unicode_bytes[i+1])[2:].encode('unicode_escape') if len(hex(unicode_bytes[i+1])) == 4 else ('0'+hex(unicode_bytes[i+1])[2:]).encode('unicode_escape')

result_bytes += b'\u'+b+a

return result_bytes.decode('unicode_escape')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值