0

I have a file which contains in each line a set of bytes for example:

4655d16c690f2789c2d3e803e388637f
16161b1504137217336d403e2a03c669
fa79c5ffe35d112915f0f3243fc68fb4
87d57d0a63e52b6df869eb5c0aac4328
640c2eefb7829d863f7aa686bc513acc
4024767c463558b7c7cd0ffd4f0aaa6d
18ee0b17f5b5206df0443e658b105990
7b40bf42d2cfc290eed4c4edcb9d3e91
b57dad9833c3e174e05a5ae75cac70ed

I want to convert line in an array,then convert byte in decimal, for example:

4655d16c690f2789c2d3e803e388637f

The result is:

46 55 d1 6c 69 0f 27 89 c2 d3 e8 03 e3 88 63 7f

Then convert each byte in decimal:

 [70,85,209,108,105,15,39,137,194,211,232,3,136,227,99,127]

I try by using this code ,

with open(Srcpath, 'r') as f:
        with open(Destpath, 'w') as fp:
            for key in f:
                key_Separated=[key[i:i+2] for i in range(0, len(key), 2)]
                rejoined = ' '.join(key_Separated)
                Decimal= [i for i, b in enumerate(rejoined ) if b=='1']
                print(Decimal)
                fp.write(str(Decimal))

so it gives this wrong results:

[43]
[21, 45]
[27, 42]
[13, 31, 37, 42]
[16, 21, 28]
[13, 36]
[12, 43, 46]
[0, 6, 18, 27, 37]

How could I correct them please?

user6652926
  • 49
  • 2
  • 9
  • https://stackoverflow.com/questions/444591/convert-a-string-of-bytes-into-an-int-python and https://stackoverflow.com/questions/5415/convert-bytes-to-floating-point-numbers-in-python – Dadep Jun 07 '17 at 13:34
  • Possible duplicate of [How do I convert hex to decimal in Python?](https://stackoverflow.com/questions/9210525/how-do-i-convert-hex-to-decimal-in-python) – mrhallak Jun 07 '17 at 13:35

2 Answers2

0

This should do the trick:

import re

data = '''4655d16c690f2789c2d3e803e388637f
16161b1504137217336d403e2a03c669
fa79c5ffe35d112915f0f3243fc68fb4
87d57d0a63e52b6df869eb5c0aac4328
640c2eefb7829d863f7aa686bc513acc
4024767c463558b7c7cd0ffd4f0aaa6d
18ee0b17f5b5206df0443e658b105990
7b40bf42d2cfc290eed4c4edcb9d3e91
b57dad9833c3e174e05a5ae75cac70ed'''

data = [re.findall('..', item) for item in data.split('\n') if item]
result = [[int(x, 16) for x in item] for item in data]
zipa
  • 27,316
  • 6
  • 40
  • 58
  • it create the correct result with an empty array all the time like this: [70, 85, 209, 108, 105, 15, 39, 137, 194, 211, 232, 3, 227, 136, 99, 127] [] – user6652926 Jun 07 '17 at 13:56
  • Just add `if item`. This happens because of the last newline in file. Edited my answer. – zipa Jun 07 '17 at 14:13
0

Python has a built-in library for the conversion you want. Given your data as data.txt:

#!python3
from binascii import unhexlify
from pprint import pprint

with open('data.txt') as f:
    pprint([list(unhexlify(line.strip())) for line in f])
[[70, 85, 209, 108, 105, 15, 39, 137, 194, 211, 232, 3, 227, 136, 99, 127],
 [22, 22, 27, 21, 4, 19, 114, 23, 51, 109, 64, 62, 42, 3, 198, 105],
 [250, 121, 197, 255, 227, 93, 17, 41, 21, 240, 243, 36, 63, 198, 143, 180],
 [135, 213, 125, 10, 99, 229, 43, 109, 248, 105, 235, 92, 10, 172, 67, 40],
 [100, 12, 46, 239, 183, 130, 157, 134, 63, 122, 166, 134, 188, 81, 58, 204],
 [64, 36, 118, 124, 70, 53, 88, 183, 199, 205, 15, 253, 79, 10, 170, 109],
 [24, 238, 11, 23, 245, 181, 32, 109, 240, 68, 62, 101, 139, 16, 89, 144],
 [123, 64, 191, 66, 210, 207, 194, 144, 238, 212, 196, 237, 203, 157, 62, 145],
 [181, 125, 173, 152, 51, 195, 225, 116, 224, 90, 90, 231, 92, 172, 112, 237]]

If using Python 2 a byte string doesn't convert into a list of integers, so there is another loop to do the conversion:

#!python2
from binascii import unhexlify
from pprint import pprint

with open('data.txt') as f:
    pprint([[ord(b) for b in unhexlify(line.strip())] for line in f])
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251