博客动态
Hi!欢迎来到Jerry Coding!喜欢请关注!谢谢!
微信扫码

扫一扫关注我

More
关于
New
RSS

ascii 字符串和16进制相互转换

Jerry 2019年7月11日 101  

       python 实现的 ascii 字符串和16进制相互转换,备忘一下。有时候转换个IP地址会用到。

'''
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                   O\ = /O
               ____/`---'\____
             .   ' \\| |// `.
              / \\||| : |||// \
            / _||||| -:- |||||- \
              | | \\\ - /// | |
            | \_| ''\---/'' | |
             \ .-\__ `-` ___/-. /
          ___`. .' /--.--\ `. . __
       ."" '< `.___\_<|>_/___.' >'"".
      | | : `- \`.;`\ _ /`;.`/ - ` : | |
        \ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='

.............................................
         佛祖保佑             永无BUG
'''

# coding:utf-8
import binascii

#ascii字符串转16进制
data = '169.254.1.1'

res = binascii.b2a_hex(str.encode(data))
print(bytes.decode(res))

#16进制转ascii字符串
data2 = '0C0D31302e322e332e3235'
res = binascii.a2b_hex(data2)
print(bytes.decode(res))

# str.encode(s)    str to bytes
# bytes.decode(b)  bytes to str 


原创文章,转载请注明出处: https://jerrycoding.com/article/ascii-2-hex

您尚未登录,暂时无法评论。请先 登录 或者 注册

0 人参与 | 0 条评论