来自config fi的Python二进制搜索模式

2024-05-16 11:29:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python做二进制搜索。就像

    Address = buff.find(b'\x99\x98\xB1\xFF')

我想让这个搜索模式通过JSON文件进行配置。所以我创建了一个变量来保存JSON文件中的数据,比如“Pattern”:“\x99\x98\xB1\xFF”

        Search_Pattern=Data_Structure[data]["Pattern"]
        print "Search Pattern:", Search_Pattern
        Address = buff.find(b'***Search_Pattern***') # this won't work. How to input the function variable here?

如何将模式从ASCII格式转换为二进制格式buff.查找功能?你知道吗


Tags: 文件数据jsonsearchaddress格式二进制模式
3条回答

数据类型转换

json被读取和解析时,该项可能会被读取为一个字符串,可以转换成^{},并将其作为参数直接传递给buff.find()。你知道吗

search_pattern_string = json_obj['pattern']
search_pattern_bytes = bytearray(search_pattern_string)
Address = buff.find(search_pattern_bytes)

更新解决方案:

JSON文件:

{"Pattern": "9707A5FF"}

部分Python代码:

conf_file = open(config_file)
conf = json.load(conf_file)
# get the search pattern
Search_Pattern=conf["Pattern"]
# convert unicode to ascii
Search_Pattern_ascii=Search_Pattern.encode('ascii','ignore')
#convert ascii string to hex data
Search_Pattern_hex=Search_Pattern_ascii.decode("hex")
#
Search_Pattern_Binary = bytearray()
Search_Pattern_Binary.extend(Search_Pattern_hex)

infile = open(Bin_file,'rb')
buff = infile.read()
Address = buff.find(Search_Pattern_Binary)

....

使用C++标准模板库的STD::vector容器,然后使用STD::Debug。然后你可以编写一个Python模块,调用C++。你知道吗

相关问题 更多 >