0

I have a file with line like this : \x04\x01\x00\x00\x00\x00\x00{ and I want to parse it like this : 04:01:00:00:00:00:00:7b, where { must be converted in hex ({ = 7b).

I'm trying to build a little python script, but the tricky part of converting some (not all) characters into hex is hard for me.

Here is my beginnig method:

def hex_parser(line):
    result = ''
    for l in line:
        if len(result):
            result = result + ':'
        if l != '\\' or l != ',' or l != '\n':
            tmp_l = l.encode('utf8').hex()
            if len(tmp_l) == 1:
                tmp_l = '0' + tmp_l
            result = result + tmp_l
    return result

Thanks for help.

2 Answers 2

2

This line will do what you ask:

result = ":".join([f"{ord(hex_value):02X}" for hex_value in line if hex_value not in ['\\','\n',',']])

This does for every character in the line, skipping those defined in a list:

  1. Converts to integer using ord. '\x00' = 0, '{' = 123
  2. Uses string formatting to to convert to a two digit hex number

Lastly it takes the list and join every part together into a string with a : between every element

EDIT:

Here is a function that parses lines that include the actual hex representation (\x00) in the text file, which results in strings like \\x00 in python

import re

def hex_parser(line):
    hex_values = []
    i = 0
    while i < len(line):
        if re.match("\\\\x[A-Fa-f0-9]{2}",line[i:i+4]):
            hex_values.append(line[i+2:i+4])
            i+= 4
        else:
            hex_values.append(f"{ord(line[i]):02X}")
            i += 1
    skip_values = [f"{ord(c):02X}" for c in ['\\','\n',',']]
    return ":".join([h for h in hex_values if h not in skip_values])

Sign up to request clarification or add additional context in comments.

6 Comments

I don't get a thing : when I use your line with a string example like this : hex_parser("\x04\x00\x00\x00\x00\x00\x00/") where hex_parser is just a return of your line, it works. But when I open the file, read it, and pass the exact same value stored in a variable, python want to convert every single characters (even the first x in \x00. I think when I pass directly the string, python interprets it like a hex string, but with the file it doesn't do the same thing
Depends on how the file looks like. When you type "\x00" in python, that means its a string of length 1 containing a null character. If you have a file containing \x00 then read that, then it will be a string of length 4 with the characters \ , x, 0 and 0
Your right, in my case the file is a txt file so every hex line is treated like x is a character ... Have you any idea to bypass this problem ? (converting the file in hex or something like this)
How does the text file actually look? Does it have \x00 or "random" characters like €eYäþšÈ[È ?
Added a function that parses \x00-like blocks in the text, hopefully that maybe help you
|
0

Try to read the file in binary mode:

with open("file.txt", "rb") as f:
    for l in f.readlines(): # reads the file line by line
        print(":".join(["%02x"%ord(c) for c in l])) # go through each character and format it as hex string, put all together with ":" inbetween

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.