[英]How would I write this from c to python?
I need to convert this to python for my use, I have a file that I want to check for this sequence and I have this code below.我需要将其转换为 python 以供我使用,我有一个要检查此序列的文件,下面有此代码。 Im thinking it will be something like this but unsure how exactly
我想它会是这样的,但不确定到底如何
pkgrep.py pkgrep.py
with open('wallet.dat') as reader:
my_data = reader.read()
while ((c = getchar()) != EOF)
if (c == 0x81 && getchar() == 0xd3 && getchar() == 0x02 && getchar() == 0x01 &&
getchar() == 0x01 && getchar() == 0x04 && getchar() == 0x20)){
for(i = 0; i < 32; i ++):
printf("%02x", getchar());
printf("\n");
}
pkgrep.c pkgrep.c
#include <stdio.h>
main() {
int c, i;
while ((c = getchar()) != EOF)
if (c == 0x81 &&
getchar() == 0xd3 && getchar() == 0x02 && getchar() == 0x01 &&
getchar() == 0x01 && getchar() == 0x04 && getchar() == 0x20) {
for (i = 0; i < 32; i++)
printf("%02x", getchar());
printf("\n");
}
}
You can search for the string in my_data
, then get the next 32 bytes with a slice.您可以在
my_data
中搜索字符串,然后使用切片获取接下来的 32 个字节。
Open the file in binary mode to ensure there's no character decoding, and search for the byte string.以二进制模式打开文件,确保没有字符解码,然后搜索字节串。
with open('wallet.dat', 'rb') as reader:
my_data = reader.read()
pos = my_data.find(b"\x81\xd3\x02\x01\x01\x04\x20")
if (pos != -1) {
for x in my_data[pos+7:pos+39]:
print(f"{x:02x}", end="")
print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.