Python Forum

Full Version: Decoding Hex / understanding Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi folks,

i'm new here and not a python crack. :)

Just started with node js and try to port a simple python application.
My problem is now, i don't understand what python is doing. I try'ed to google the syntax, but no chance to get clear on this.

So i have to ask you, if you're able to help me making so progress on this. :)

with open(sys.argv[1], "rb") as f:
    for line in f:
        for member in members:
            if '   '+member+'.' in line:
                data = ''.join(islice(f,1,2)).replace(" ", "")
                #print member, data
                might = (struct.unpack("<I", codecs.decode(data[4:12], "hex"))[0])/1000
                score = struct.unpack("<h", codecs.decode(data[12:16], "hex"))[0]
                results[member] = str(score) + '\t' + str(might)

for member in members:
    try:
        #print member +'\t', results[member]
        print results[member]
    except:
        print member, 'NOT FOUND'
And here is an example of the data this app normally should parse:


0000  f8 01 2e 0c 00 00 00 00 f2 9b 9f 0a 00 00 00 00   ................
0010  32 40 02 00 00 00 2d 00 1a 41 e1 06 00 00 00 00   [email protected]......
0020  73 63 68 72 75 62 62 69 f0 9f 87 a9 f0 9f 87 aa   schrubbi........
0030  00 8f d1 8f d1 8f 00 00 00 00 00 00 00 00 00 00   ................
0040  a5 2b 05 00 d4 0d 00 00 c6 f2 c8 06 00 00 00 00   .+..............
0050  4d 63 54 53 00 bc d0 bf d0 be d0 bc 00 bd d0 b3   McTS............
0060  00 b0 00 b9 00 00 00 00 00 00 00 00 00 00 00 00   ................
0070  80 8e 04 00 bc 0d 00 00 e6 ad 64 09 00 00 00 00   ..........d.....
0080  2d 2d 2d 6d 61 6a 6f 72 2d 2d 2d 00 31 00 8f bf   ---major---.1...
0090  00 80 d0 be d1 87 00 00 00 00 00 00 00 00 00 00   ................
00a0  7a 42 04 00 3e 0d 00 00 73 3d 5f 09 00 00 00 00   zB..>...s=_.....
00b0  2d 2d 2d 53 6f 66 74 6a 6f 6b 65 2d 2d 2d 00 91   ---Softjoke---..
00c0  d0 be d0 b3 00 00 8f 00 00 00 00 00 00 00 00 00   ................
00d0  3a 1b 04 00 07 0d 00 00 17 87 4f 0a 00 00 00 00   :.........O.....
00e0  59 56 4f 56 59 00 67 66 67 6a 68 76 67 67 00 f0   YVOVY.gfgjhvgg..
00f0  9f 8e 83 00 8f bb 00 00 00 00 00 00 00 00 00 00   ................
0100  a2 23 04 00 ee 0c 00 00 ac 0f b8 08 00 00 00 00   .#..............
0110  61 75 74 68 6f 58 4e 00 42 65 61 73 74 00 d0 91   authoXN.Beast...
0120  d1 83 d0 bb d1 8c 00 00 00 00 00 00 00 00 00 00   ................
0130  d3 2a 04 00 dc 0c 00 00 f2 70 4b 0d 00 00 00 00   .*.......pK.....
0140  53 45 4e 47 45 52 53 4f 4e 00 b5 d0 b1 d0 b0 d0   SENGERSON.......
0150  bd d0 b0 d1 8f 00 29 00 00 00 00 00 00 00 00 00   ......).........
0160  00 48 04 00 d4 0c 00 00 cf 8c 27 13 00 00 00 00   .H........'.....
0170  55 65 62 65 72 62 6f 73 73 00 3a 31 39 39 33 00   Ueberboss.:1993.
0180  d1 80 d1 82 d0 b5 2e 00 00 00 00 00 00 00 00 00   ................
0190  ce 7d 04 00 cb 0c 00 00 58 99 7e 09 00 00 00 00   .}......X.~.....
01a0  69 6c 65 6d 61 63 63 00 49 55 4d e2 80 9d 00 8f   ilemacc.IUM.....
01b0  d0 be d0 b8 00 bd 00 00 00 00 00 00 00 00 00 00   ................
01c0  ba da 03 00 91 0c 00 00 99 b7 bf 0c 00 00 00 00   ................
01d0  42 6c 65 63 68 74 69 74 74 65 00 bb d0 b0 36 34   Blechtitte....64
01e0  00 bf d0 ba d0 b0 00 00 00 00 00 00 00 00 00 00   ................
01f0  8a 90 03 00 e9 0b 00 00                           ........
Hope you can help me Angel

Kind regards Hand


EDIT:

Here is the repo of the python script:
https://github.com/Sigtran/CC_GW_Parser/...rse.py#L74
Well, that's clearly not the whole file. If you tried to run it, I'm sure it would throw an error lol.

But it looks like it's reading a binary/hex file, and looking for certain things in that file, decoding them, and displaying them in an easier to read way.
Hm! Opened as a binary file and read it line by line?! Think
@nilamo

Thats right, i posted a link to the repo where you'll find the complete code.

@wavic

So it decodes a hex file and reads binary?

The result i get from the file i read though the script is something like:

nick - might - points
YVOVY - 292920 - 2940
schrubbi - 338465 - 3750

and so on...
As far as i understand the coding, i thought there is something calculated, or?
might = (struct.unpack("<I", codecs.decode(data[4:12], "hex"))[0])/1000
maybe you can help me break this line up in pieces like:

struct.unpack -> decodes hax
- <I -> i dont know :)
- codecs.decode(data[4:12], -> what is about data 4:12 are these position in the hex?
- etc...

EDIT: I also tried to view the file with a hex editor and find the final output as clear-text. No chance :)

Thank you guys for helping me here.
Regards,
Axel
Ok, what i know for now:

"<I" -> "<" little-endian byte order / "I" character format integer (size 4) (c: unsigned int)

Although i know know what this is for, im unable to understand what exactly it does :P