0

i am searching fo a way to get a Hex Number from a line.

It is a bigger file and i can find the number this way:

The line starts with ".text" (it is the first one which starts with .text). This line contains 3 HexNumbers and i need everytime the second one.

The Problem is that the positions of the numbers are not everytime the same, also the length is not everytime the same.

The line looks like

.text        0x00000        0x4e32     0x11000000

between the numbers there are several spaces, not same everytime.

How can i get and save the 0x4e32 ?

I tried to start like this:

for num, line in enumerate(mapfile, 1):
    line = line.rstrip()
    if line.startswith('.text '):
        foundLine = line
        strCheck = "0x"
        lineNumber = num
        col = foundLine.find(strCheck)+1
        print lineNumber, col           

        index = 0
        maxLen = 60
        while index < len(line):
            index = line.find('0x', index)
            if index == -1:
                break
        print('Code Size found at', index)
        index += 2

3 Answers 3

1

Once a line starts with .text, split it and the third item will be your target value:

with open('large_file.txt') as f:
   for number,line in enumerate(f):
       if line.startswith('.text'):
           bits = line.split()
           try:
              print('Found at line # {} - {}'.format(number, bits[2]))
           except IndexError:
              print('Malformed line: {}'.format(line))
Sign up to request clarification or add additional context in comments.

Comments

1

The .split() method without an argument splits a string at whitespace. This means that any number of spaces will do, and the behavior will be the same with one or a hundred spaces between the words. Then just regularly access with the index of the word you want.

e.g.

words = line.split()
words[2]

Comments

0
import re
p = re.compile(r'^\.text\s*\S+\s*(\S+)', re.MULTILINE)
test_str = ".text 0x00000 0x4e32 0x11000000"

re.findall(p, test_str)

You can iterate over lines apply this regex to each line and get your result.If the file is not big then you can load the while file in memory and then appy regex to get results in one go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.