首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python 字符串转16进制数字

python 字符串转16进制数字

作者头像
全栈程序员站长
发布2022-08-01 15:14:56
发布2022-08-01 15:14:56
3.8K0
举报

大家好,又见面了,我是你们的朋友全栈君。

1 原始文件中的字符串

2 读取文件字符串

从文件中读取2个字节,代码如下:

def print_hex_str(str1): print len(str1) print str1 print int(str1, 16) for i in str1: print “——–“ print(‘%#X’ % ord(i)) print(‘%d’ % ord(i))

def des_ecb_decrypt_from_file(srcfile, dstfile, key): fsrc = open(srcfile, ‘rb’) if not fsrc: print “fsrc open failed!” fdst = open(dstfile, ‘wb’) if not fdst:

print “fdst open failed!”

datalen = fsrc.read(2) print type(datalen) print len(datalen)

print_hex_str(datalen)

3 运行程序报错

4 对字符串进行binascii转换

5 字符串转为整型正常

原字符串为:0X000X58

转换后的字符串为:0058

6 binascii分析

binascii.b2a_hex(data) 字符串转16进制字符串binascii.hexlify(data)

Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.

官方网址:https://docs.python.org/2/library/binascii.html

7 参考资料

(1) https://blog.csdn.net/penny_hardaway/article/details/45046643

(2) https://www.cnblogs.com/LarryGen/p/5088144.html

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125941.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档