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 ...
随机推荐
- JPG学习笔记2(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- Lenet车牌号字符识别+保存模型
# 部分函数请参考前一篇或后一篇文章 import tensorflow as tf import tfrecords2array import numpy as np import matplotl ...
- Worktile vs Teambition
Worktile vs Teambition 项目管理.团队协作 企业服务.协同办公 worktile 易成科技 北京易成星光科技有限公司 https://www.tianyancha.com/com ...
- 神奇的数学学习网站 All In One
神奇的数学学习网站 All In One magical math websites {{uploading-image-923797.png(uploading...)}} Math is Fun ...
- how to create a ring progress bar in web skills
how to create a ring progress bar in web skills ring progress bar & circle progress bar canvas j ...
- NGK” 呼叫河马 “智能合约火爆全网
最近有一款基于NGK.IO公链上的智能合约"呼叫河马"在区块链市场很火.通过访问和查阅资料可知,"呼叫河马"是一款全新的智能合约Dapp小游戏,智能合约代码是1 ...
- C语言指针基本知识
对程序进行编译的时候,系统会把变量分配在内存单位中,根据不同的变量类型,分配不同的字节大小.比如int整型变量分配4个字节,char字符型变量分配1个字节等等.被分配在内存的变量,可以通过地址去找到, ...
- 微信小程序中input标签高度设置
如果没有设置高度所以显示的是控件自身的高度. 微信小程序input控件原始设置: 上图发现: 我只覆盖了官方input的height,而没有覆盖min-height; .query input{ bo ...
- 创建时间和更新时间两个选一个的情况和select case when ... then ... else ... end from 表 的使用
1.查询时间,如果更新时间update_time为空就查创建时间create_time,否则查更新时间update_time select update_time,create_time, case ...
- 痞子衡嵌入式:自识别特性(Auto Probe)可以让i.MXRT1060无需FDCB也能从NOR Flash启动
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是自识别特性(Auto Probe)可以让i.MXRT1060无需FDCB也能从NOR Flash启动. 接着上篇文章 <了解i.M ...