十六进制数据流解析

 十六进制数据流一般是我们在进行抓包或者接口进行了加密后,接口返回的数据信息,我们需要对其进行特殊处理后才是我们能直观识别的信息,我们就用Python语言进行解析,这里需要用到以下几个函数。

函数名实例打印解释
hexhex(0x7b)"0x7b"用于将10进制整数转换成16进制,以字符串形式表示.
intint("0x7b",16)123用于将字符串转换成整数型.
chrchr(123)"{"用于整数型或者16进制转换成对应的ascii字符.
isascii"{".isascii()True若字符含有ascii字符则返回真.
binasciibinascii.a2b_hex("e590af")b'\xe5\x90\xaf'16进制字符串转换成2进制数据表示.
decodeb'\xe5\x90\xaf'.decode()指定编码格式默认为UTF-8
  • 注意:chr(0x7b)我们可以直接转成ascii字符,为何我们要介绍那么多呢,因为我们接收到的数据里面可能含有中文字符,所以我们要先进行整数型转换,然后再分流转换。

  • 代码示例如下所示:

#!/usr/bin/python
# -*- coding: utf-8 -*-

#@Users: LiMu
#@Files:SixteenAnalysis.py
#@Times: 2022/11/29 
#@Software:PyCharm

import os
import sys
import time
import binascii

__curr_dir = os.path.dirname(os.path.abspath(__file__))
if __curr_dir not in sys.path:
    sys.path.insert(0, __curr_dir)


def to_string(source: list):
    cour = 0
    rlt = []
    source_tmp = [hex(i) for i in source]

    while cour < len(source):
        ele = chr(int(source_tmp[cour], 16))
        if ele.isascii():
            rlt.append(ele)
            cour += 1
        else:
            tmp = [x[2:] for x in source_tmp[cour: cour + 3]]
            rlt.append(binascii.a2b_hex("".join(tmp)).decode())
            cour += 3

    print("".join(rlt))


if __name__ == '__main__':
    arr = [
        0x7b, 0x22, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x3a, 0x22, 0x63, 0x6f, 0x64,
        0x65, 0x5f, 0x31, 0x36, 0x36, 0x39, 0x36, 0x39, 0x30, 0x31, 0x34, 0x31, 0x32, 0x35, 0x33, 0x22, 0x2c, 0x22,
        0x50, 0x75, 0x73, 0x68, 0x4b, 0x65, 0x79, 0x22, 0x3a, 0x6e, 0x75, 0x6c, 0x6c, 0x2c, 0x22, 0x52, 0x65, 0x73,
        0x75, 0x6c, 0x74, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x3a, 0x31, 0x32, 0x37, 0x35, 0x2c, 0x22, 0x53,
        0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x3a, 0x31, 0x32, 0x37, 0x35, 0x2c, 0x22, 0x53, 0x74, 0x61, 0x74, 0x75,
        0x73, 0x4d, 0x73, 0x67, 0x22, 0x3a, 0x22, 0xe6, 0xb4, 0xbb, 0xe5, 0x8a, 0xa8, 0xe6, 0x9c, 0xaa, 0xe5, 0xbc,
        0x80, 0xe5, 0x90, 0xaf, 0x22, 0x2c, 0x22, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x3a, 0x6e, 0x75, 0x6c, 0x6c,
        0x2c, 0x22, 0x54, 0x69, 0x6d, 0x65, 0x54, 0x69, 0x63, 0x6b, 0x22, 0x3a, 0x31, 0x36, 0x36, 0x39, 0x36, 0x39,
        0x30, 0x31, 0x37, 0x36, 0x2c, 0x22, 0x45, 0x78, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x3a, 0x6e, 0x75,
        0x6c, 0x6c, 0x7d
    ]
    to_string(arr)
  • 执行打印:

D:\Python37\python.exe E:/pythonFiles/Interfacetest/ShellEditing/sixteenAnalysis.py
{"HandleCode":"code_1669690141253","PushKey":null,"ResultStatus":1275,"Status":1275,"StatusMsg":"活动未开启","Value":null,"TimeTick":1669690176,"ExtValue":null}

Process finished with exit code 0



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值