【问题标题】:Formatting a string to be the same as everything else将字符串格式化为与其他所有内容相同
【发布时间】:2022-01-09 07:21:57
【问题描述】:

我的文字如下所示:

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

如何动态更改每个字符串的格式,以使每行指令的助记符位于同一区域以提高可读性?

【问题讨论】:

  • 您可以动态指定格式宽度,例如f"{mystring:<{width}}" 如果有帮助,一旦你计算了宽度(在这个例子中是变量 width)。

标签: python string formatting


【解决方案1】:

这将按照您的要求格式化 line

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

【讨论】:

    【解决方案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)
    

    输出:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多