问题代码:

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的更多相关文章

  1. c#字符编码,System.Text.Encoding类,字符编码大全:如Unicode编码、GB18030、UTF-8,UTF-7,GB2312,ASCII,UTF32,Big5

    本页列出来目前window下所有支持的字符编码  ---c#通过 System.Text.Encoding.GetEncodings()获取,里面可以对其进行查询,筛选,对同一个字符,在不同编码进行查 ...

  2. System.Text.Encoding.Default

    string strTmp = "abcdefg某某某";int i= System.Text.Encoding.Default.GetBytes(strTmp).Length;/ ...

  3. System.Text.Encoding.cs

    ylbtech-System.Text.Encoding.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77 ...

  4. 【参考】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, ...

  5. UnicodeMath数学公式编码_翻译(Unicode Nearly Plain - Text Encoding of Mathematics Version 3)

    目录 完整目录 1. 简介 2. 编码简单数学表达式 2.1 分数 2.2 上标和下标 2.3 空白(空格)字符使用 3. 编码其他数学表达式 3.1 分隔符 强烈推荐本文简明版UnicodeMath ...

  6. sublime text 2 解决错误 [Decode error - output not utf-8]

    以win 10 为例, 找到文件C:\Users\xxzx\AppData\Roaming\Sublime Text 2\Packages\Python\Python.sublime-build 添加 ...

  7. ASP.NET 返回字符串 IE6乱码问题

    项目A,所有的文件编码和内容编码都是UTF-8. 项目B,Index.aspx文件编码和页面内容编码都是GB2312. 项目A返回JSON格式数据给项目B时,其它浏览器都可以就是IE不行.后来在网上找 ...

  8. .net core2.2

    GetCurrentDirectory returns the worker directory of the process started by IIS rather than the app's ...

  9. 一个免费ss网站的数据爬取过程

    一个免费ss网站的数据爬取过程 Apr 14, 2019 引言 爬虫整体概况 主要功能方法 绕过DDOS保护(Cloudflare) post中参数a,b,c的解析 post中参数a,b,c的解析 p ...

随机推荐

  1. 2015 - 2020 最新 Linux 命令大全

    # 2015 - 2020 最新 Linux 命令大全 ## VIM 命令模式(Command mode):vi 插入模式(Insert mode):i底线命令模式(Last line mode):e ...

  2. Taro 框架实现原理

    Taro 框架实现原理 小程序 H5 RN Web 多端框架 Taro 1.x & Taro 2.x 编译型架构, 语法编译转换 Taro 1/2 属于编译型架构,主要通过对类 React 代 ...

  3. GitHub Classroom

    GitHub Classroom GitHub Education https://classroom.github.com/classrooms https://classroom.github.c ...

  4. es6 & map & set

    es6 & map & set Map & WeakMap https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re ...

  5. export excel

    export excel sheet.js https://sheetjs.com/ https://github.com/SheetJS/sheetjs excel.js https://www.n ...

  6. NGK乘势而上打造生态所,建立全方位的区块链生态系统

    当金融理财变成了生活的一部分,购买金融衍生品的眼光成为了影响生活质量重要组成部分.这是一个不缺少黄金的年代,一夜间实现财务自由的故事每天都在上演,但是由于太多人缺少发现黄金的眼睛,只能被财富和机遇拒之 ...

  7. VAST算力增值效应,助力NGK全生态产业链!

    虽然比特币和区块链在2009年就诞生了,但它们对于一些人来说好像还是很遥远,归根结底还是由于数字货币始终未能在全球真正实现流通和支付功能.区块链1.0,以比特币为代表,实现了数字支付:区块链2.0,E ...

  8. [转]ORB特征提取-----FAST角点检测

    转载地址:https://blog.csdn.net/maweifei/article/details/62887831 (一)ORB特征点提取算法的简介 Oriented FAST and Rota ...

  9. redis缓存穿透穿透解决方案-布隆过滤器

    redis缓存穿透穿透解决方案-布隆过滤器 我们先来看一段代码 cache_key = "id:1" cache_value = GetValueFromRedis(cache_k ...

  10. [Python] Matplotlib 图表的绘制和美化技巧

    目录 在一张画布中绘制多个图表 加图表元素 气泡图 组合图 直方图 雷达图 树状图 箱形图 玫瑰图 在一张画布中绘制多个图表 Matplotlib模块在绘制图表时,默认先建立一张画布,然后在画布中显示 ...