LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs
问题代码:
b=b'\x01\x02\x03'
x=binascii.b2a_hex(b.decode('hex')[::-1].encode('hex'))
python2下是不报错的,因为python2内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,
即先将其他编码的字符串解码(decode)成unicode (str-->decode()-->bytes),再从unicode编码(encode)成另一种编码(bytes-->encode()-->str)。
python3报错,因为python3内部表示为utf-8
实现读小端序显示十六进制记录
1 import binascii
2
3 f=open("d:\\text","wb")
4 f.write(bytes([0x34,0x12]))
5 f.close()
6 f = open("d:\\text", "rb")
7
8 #实现读小端序显示十六进制
9 #way1 通过binascii包下方法
10 x=binascii.b2a_hex(f.read(2)[::-1])
11 print(x.decode())
12
13 f.seek(0)
14 #way2 使用int.from_bytes()转为int
15 y = int.from_bytes(f.read(2),byteorder='little',signed='false')
16 print(hex(y))
17 f.close()
18
19
20
21 #结果
22 1234
23 0x1234
LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs的更多相关文章
- c#字符编码,System.Text.Encoding类,字符编码大全:如Unicode编码、GB18030、UTF-8,UTF-7,GB2312,ASCII,UTF32,Big5
本页列出来目前window下所有支持的字符编码 ---c#通过 System.Text.Encoding.GetEncodings()获取,里面可以对其进行查询,筛选,对同一个字符,在不同编码进行查 ...
- System.Text.Encoding.Default
string strTmp = "abcdefg某某某";int i= System.Text.Encoding.Default.GetBytes(strTmp).Length;/ ...
- System.Text.Encoding.cs
ylbtech-System.Text.Encoding.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77 ...
- 【参考】IBM sun.io.MalformedInputException and text encoding conversions transforms numerals to their word equivalents - United States
Problem(Abstract) When converting contents from a file or string using WebSphere Application Server, ...
- UnicodeMath数学公式编码_翻译(Unicode Nearly Plain - Text Encoding of Mathematics Version 3)
目录 完整目录 1. 简介 2. 编码简单数学表达式 2.1 分数 2.2 上标和下标 2.3 空白(空格)字符使用 3. 编码其他数学表达式 3.1 分隔符 强烈推荐本文简明版UnicodeMath ...
- sublime text 2 解决错误 [Decode error - output not utf-8]
以win 10 为例, 找到文件C:\Users\xxzx\AppData\Roaming\Sublime Text 2\Packages\Python\Python.sublime-build 添加 ...
- ASP.NET 返回字符串 IE6乱码问题
项目A,所有的文件编码和内容编码都是UTF-8. 项目B,Index.aspx文件编码和页面内容编码都是GB2312. 项目A返回JSON格式数据给项目B时,其它浏览器都可以就是IE不行.后来在网上找 ...
- .net core2.2
GetCurrentDirectory returns the worker directory of the process started by IIS rather than the app's ...
- 一个免费ss网站的数据爬取过程
一个免费ss网站的数据爬取过程 Apr 14, 2019 引言 爬虫整体概况 主要功能方法 绕过DDOS保护(Cloudflare) post中参数a,b,c的解析 post中参数a,b,c的解析 p ...
随机推荐
- vue 在有大数据量的 table 中使用弹窗 input 输入数据时卡顿解决方案
vue 在有大数据量的 table 中使用弹窗 input 输入数据时卡顿解决方案 原因:vue在进行输入时,进行了多次的render刷新渲染操作,导致了input框输入时发生的卡顿现象 解决方法:在 ...
- Battery API All In One
Battery API All In One https://caniuse.com/?search=Battery navigator.getBattery() /* Promise {<pe ...
- 最新 React 源码学习笔记
最新 React 源码学习笔记 v17.x.x 框架架构 核心算法 设计模式 编码风格 项目结构 为什么出现 解决了什么问题 有哪些应用场景 refs https://github.com/learn ...
- PM2 All In One
PM2 All In One https://pm2.keymetrics.io/ https://pm2.io/ $ yarn global add pm2 # OR $ npm install p ...
- react & redux data flow diagram
react & redux data flow diagram Redux 数据流程图
- disable html input & pointer-events
disable html input & pointer-events css https://developer.mozilla.org/en-US/docs/Web/CSS/pointer ...
- post upload file & application/x-www-form-urlencoded & multipart/form-data
post upload file application/x-www-form-urlencoded & multipart/form-data https://stackoverflow.c ...
- 新年狂欢高倍币,1万枚VAST拿到手软
新一年NGK推出了新币VAST,成为了当前最瞩目的一颗星.有人认为VAST有价无市,有人认为VAST价值巨大,潜力无限.总之,众说风云! 选择数字货币,当然是挑选有价值的货币,从内外价值入手. 一.币 ...
- NGK公链脱颖而出,成为值得期待的项目!
当下2020年是动荡的一年,全世界经济危机汲汲可危,在这个特殊的时刻,有人抱怨说这是最坏的年代,也有人庆幸说这是最好的年代,历史不会重演,但总是惊人的相似,首先带你回顾一下上一次金融危机出现的2008 ...
- node初体验(二)
1.静态资源访问,需要设置路由和响应标头 2.url模块.path模块.querystring模块 Url { protocol: null, slashes: null, auth: null, h ...