python 字符串转16进制数字

1原始文件中的字符串2读取文件字符串从文件中读取2个字节,代码如下:defprint_hex_str(str1):printlen(str1)printstr1printint(str1,16)foriinstr1:print"——–"print(‘%#X’%ord(i))print…

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

1 原始文件中的字符串

python 字符串转16进制数字

 

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 运行程序报错

python 字符串转16进制数字

python 字符串转16进制数字

 

4 对字符串进行binascii转换

python 字符串转16进制数字

5 字符串转为整型正常

python 字符串转16进制数字

原字符串为: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

python 字符串转16进制数字

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2022年4月18日 上午11:00
下一篇 2022年4月18日 上午11:00


相关推荐

  • Python 中 str.format() 方法详解

    Python 中 str.format() 方法详解Python 中 str format 方法详解文章目录 Python 中 str format 方法详解 0 参考资料 1 术语说明 2 简单字段名 2 1 简单字段名的说明 2 2 省略字段名 2 3 数字形式的简单字段名 2 4 变量名形式的简单字段名 2 5 简单字段名的混合使用 2 6 使用元组和字典传参 3 复合字段名 3 1 复合字段名的说明 3 2 点号的使用 3 3 方

    2026年3月26日
    2
  • linux下重启网络服务的命令

    linux下重启网络服务的命令linux 下重启网络服务的命令配网卡的时候 用到了下面的命令重启网络服务的命令是 servicenetwo 或者 etc init d networkresta 在 linux 下更改 ip 后如果没有重启网络服务实际上是不发生变化的

    2026年3月26日
    2
  • gridview属性_datagridview设置列宽

    gridview属性_datagridview设置列宽usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;usingSystem.Windows.Forms;classSetDataViewGirdStyle{   privatestaticColor_mLinearBeginColor;

    2026年3月4日
    3
  • js array 数组添加与删除数据

    js array 数组添加与删除数据//新建数组varids=newArray();//向数组中添加数据ids.push(1);//删除下标为i的数组元素ids.remove(i);//自定义删除函数:Array.prototype.remove=function(dx){if(isNaN(dx)||dx>this.length){returnfalse;}…

    2022年6月10日
    54
  • ubuntu安装eclipse及问题

    ubuntu安装eclipse及问题因为要用 eclipse 做 NS3 C 的仿真 所以下了个 eclipse 我发现我只要下一个软件 但凡是跟学习沾点边 他都会翻车 哭了 命运这是在暗示什么吗 系统 ubuntu18 04 win10 双系统 jdk 版本 1 8 0 282 也就是软件包 jdk 8u linux x64 tar gzeclipse 版本 其中 jdk 的版本比较重要 因为要和 eclipse 适配 否则就会出现 jdk 下载好 环境变量配置好 eclipse 也下载好了 但就是打不开的情况 很让人无奈 一 下载安装 JDK 可以

    2026年3月17日
    2
  • PyCharm配置教程[通俗易懂]

    PyCharm配置教程[通俗易懂]一、安装进入pyCharm官网:https://www.jetbrains.com/pycharm。根据自己的操作系统选择相应的下载包。其中专业版是要付费的,可以自行购买,也可以google搜索激活成功教程教程。下载后根据提示安装即可二、配置1.设置主题以及字体大小点击PyCharm,进入preferences选项设置更改主题以及字体颜色2.配置属于自己的文件模板举个例子,如果你要创建一个python文件,那么你可能需要类似这样的代码注释在文件的最上方文件代码注释如果我们

    2022年8月26日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号