簡體   English   中英

將字符串格式化為與其他所有內容相同

[英]Formatting a string to be the same as everything else

我有這樣的文字:

0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd  34
0x00000023 2f das  35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd  44
0x0000002d 90 nop  45
0x0000002e 90 nop  46
0x0000002f 90 nop  47

無論呼叫有多大,我都想將字符串(第 3 位)中的助記符格式化為與呼叫(第 2 位)相同的空間。 例如:

0x00000000 ebc4         jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a   mov esp,0x7ab8d9eb 8
0x0000000d b182         mov cl,130 13

以上是我需要做的完美示例。 我嘗試過使用字符串格式化技術,例如"{} {:<10} {:>10} {}\n" ,但是這並不完全像我預期的那樣看起來像這樣:

0x00000000 ebc4            jmp 0x-0000003a 0
0x00000002 30b9aaad03f0    xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a      mov esp,0x7ab8d9eb 8
0x0000000d b182                 mov cl,130 13
0x0000000f 3bbd98607e3c    cmp edi,dword [ebp + 1014915224] 15
0x00000015 3229            xor ch,byte [ecx] 21
0x00000017 3c01                   cmp al,1 23
0x00000019 25040bbe7d      and eax,0x7dbe0b04 25
0x0000001e 13fd                adc edi,ebp 30
0x00000020 b48d                 mov ah,141 32
0x00000022 af                       scasd  34
0x00000023 2f                         das  35
0x00000024 34a4                 xor al,164 36
0x00000026 02929d1302a3    add dl,byte [edx - 1560144995] 38
0x0000002c 9c                      pushfd  44
0x0000002d 90                         nop  45
0x0000002e 90                         nop  46
0x0000002f 90                         nop  47

如何動態更改每個字符串的格式,以便將指令的助記符放在每行的同一區域以提高可讀性?

這將按照您的要求格式化line

"%s %-12s %s" % tuple(line.split(' ',2))

這是一個帶有循環的解決方案:

s = "0x00000000 ebc4 jmp 0x-0000003a 0\n0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2\n0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8\n0x0000000d b182 mov cl,130 13\n0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15\n0x00000015 3229 xor ch,byte [ecx] 21\n0x00000017 3c01 cmp al,1 23\n0x00000019 25040bbe7d and eax,0x7dbe0b04 25\n0x0000001e 13fd adc edi,ebp 30\n0x00000020 b48d mov ah,141 32\n0x00000022 af scasd  34\n0x00000023 2f das  35\n0x00000024 34a4 xor al,164 36\n0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38\n0x0000002c 9c pushfd  44\n0x0000002d 90 nop  45\n0x0000002e 90 nop  46\n0x0000002f 90 nop  47"
lines = s.split("\n")
split_lines = [line.split() for line in lines]

new_lines = [split[0] + " " + split[1] for split in split_lines]
for i, line in enumerate(new_lines):
    while len(line) < 24:
        line += " "
    line += split_lines[i][2] + " " + split_lines[i][3]
    new_lines[i] = line
    
for line in new_lines:
    print(line)

Output:

0x00000000 ebc4         jmp 0x-0000003a
0x00000002 30b9aaad03f0 xor byte
0x00000008 bcebd9b87a   mov esp,0x7ab8d9eb
0x0000000d b182         mov cl,130
0x0000000f 3bbd98607e3c cmp edi,dword
0x00000015 3229         xor ch,byte
0x00000017 3c01         cmp al,1
0x00000019 25040bbe7d   and eax,0x7dbe0b04
0x0000001e 13fd         adc edi,ebp
0x00000020 b48d         mov ah,141
0x00000022 af           scasd 34
0x00000023 2f           das 35
0x00000024 34a4         xor al,164
0x00000026 02929d1302a3 add dl,byte
0x0000002c 9c           pushfd 44
0x0000002d 90           nop 45
0x0000002e 90           nop 46
0x0000002f 90           nop 47

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM