A-A+

python提取wireshark十六进制中的汉字

2024年05月21日 11:38 学习笔记 暂无评论 共273字 (阅读63 views次)

【注意:此文章为博主原创文章!转载需注意,请带原文链接,至少也要是txt格式!】

不说废话直接上Python脚本

 

def code_replace_from(code):
    code = code.replace(" ", "")  # 去除空格  没有使用前后特殊字符去除.strip()
    bytes_data = bytearray.fromhex(code)

    decoded_str = ""
    i = 0
    while i < len(bytes_data):
        # Check if the current byte is a potential start of a UTF-8 character
        if bytes_data[i] >= 0xE0 and bytes_data[i] <= 0xEF:
            try:
                # Try to decode the next three bytes as a UTF-8 character
                char = bytes_data[i:i + 3].decode('utf-8')
                decoded_str += char
                i += 3
            except UnicodeDecodeError:
                i += 1
        else:
            i += 1
    return decoded_str

 
使用示例:

 

b = "02 64 02 98 02 0F 00 18 36 36 32 38 37 61 61 32 38 64 63 36 62 62 34 32 38 39 30 30 34 39 31 32 00 00 00 68 00 00 03 EC 00 0D 8D 4F 00 06 E9 BE 99 E5 93 A5 00 02 FE D2 00 18 36 36 32 61 38 35 37 32 63 62 62 33 38 36 30 35 65 37 30 30 31 62 37 37 00 0B 38 38 34 31 36 37 31 35 30 31 39 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 36 36 31 66 66 32 33 33 38 64 63 36 62 62 31 38 36 66 30 30 30 62 63 33 00 00 00 48 00 00 03 EC 00 0D 8D 4E 00 06 E6 A4 B0 E5 AD 90 00 08 C7 2E 00 18 36 36 31 66 66 38 37 37 38 64 63 36 62 62 31 38 36 66 30 30 31 33 32 35 00 0B 38 38 34 31 36 31 31 33 35 32 32 00 01 01 00 01 01 00 01 63 FB 00 00 00 00 00 00 00 09 01 00 00 00 00 00 00 00 00 40 B3 83 00 00 00 00 00 41 14 E6 10 00 00 00 00 00 00 00 00 01 00 09 3B 49 00 09 3B 49 00 D2 06 05 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 11 A4 E0 00 00 00 00 00 00 00 00 01 00 06 2E 13 00 06 2E 13 00 D2 04 05 00 03 40 D5 E1 40 00 00 00 00 00 00 00 00 00 00 00 00 41 12 6E A0 00 00 00 00 00 00 00 01 01 00 06 2E 29 00 06 2E 29 00 D2 04 05 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 12 AB 4C 00 00 00 00 00 00 00 00 01 00 07 B4 AD 00 07 B4 AD 00 D2 05 05 00 05 40 BA 9C 00 00 00 00 00 40 B4 AC 00 00 00 00 00 41 10 BD CC 00 00 00 00 00 00 00 00 01 00 04 A7 74 00 04 A7 74 00 D2 03 05 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 10 9A 90 00 00 00 00 00 00 00 00 01 00 01 9A 4A 00 01 9A 4A 00 D2 01 05 00 08 41 3B 3F 4E 00 00 00 00 41 0B 8D 08 00 00 00 00 40 DC 88 40 00 00 00 00 00 00 00 01 00 00 04 C6 B3 00 04 A7 73 00 F7 03 0D 00 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 EC 00 3C 00 00 00 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 EC 00 3C 00 00 00 00 01 30 00 00 00 02 00 0E 00 56 00 00"
shiliu = code_replace_from(b)
print(shiliu)

把字符串转换为十六进制如下:

text = "111我们的祖国是花园"

# Convert the text to UTF-8 encoded bytes
utf8_bytes = text.encode('utf-8')

# Convert each byte to its hexadecimal representation and join them together
hex_string = ''.join(f'{byte:02X}' for byte in utf8_bytes)

print(hex_string)

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×

给我留言