Reputation: 809
I have the following hex values, which i have in a file.
ff00 928a 6275 7474 6f6e 4c69 7374 df00
84ff 002a 8869 6e70 7574 5069 6e41 0185
426e 616d 6588 4a41 4a41 4a41 4441 8672
656c 6179 73c8 4103 4103 4103 4103 ff00
2a88 696e 7075 7450 696e 4102 8542 6e61
6d65 884e 454a 4e45 4a44 4186 7265 6c61
7973 c841 0441 0441 0441 04ff 0027 8869
6e70 7574 5069 6e41 0385 426e 616d 6585
4d41 4144 4186 7265 6c61 7973 c841 0141
0141 0141 01
I want to split these hex values up in single bytes like ff and add the leading zero 0x, so i can put it directly into a unsigned char array in C.
I've tried by loading the hex values into python and removed all whitespaces and afterwards setting a whitespace by every other char so they're represented as single bytes. But no matter what i can't seem to change the layout of the hex values.
Here's the code
with open ("yes.txt", "r") as myfile:
byte=myfile.read().replace('\n', '').replace(' ','')
[byte[i:i+2] for i in xrange(1,len(byte))]
print byte
print byte
Can someone please help me.
Thanks in advance
Upvotes: 2
Views: 3146
Reputation: 2424
Assuming you want to read the values from a file and write them back in the new format that is, from a file of content "ff00 928a" get a new file with the content "0xff 0x00 0x92 0x8a" this code should do the trick:
with open("vals.txt") as f:
hexVals = f.read()
with open("out.txt", "w") as g:
for val in hexVals.split():
g.write('0x' + val[:2] + " ")
if(len(val) > 2):
g.write('0x' + val[2:] + " ")
EDIT
If the file is binary (when you print f.read() you don't see the values you give but what they represent in ascii, you can do
from binascii import hexlify
hexVals = hexlify(f.read())
see if printing this gives you a string with the hex representation of the data. And of course, you can convert it back with unhexlify from the same module, if needed.
Upvotes: 3
Reputation: 365915
Your list comprehension does return something useful—a list of every pair of characters.
It does have a couple problems, though: You're returning every pair of characters, 1-2, then 2-3, then 3-4. You want to use a step
argument to make sure the range is distinct pairs, rather than overlapping pairs. And you want to start from 0, not 1, so you don't skip the first character.
More importantly, you don't do anything with that list, so that isn't very helpful.
The first thing to do is assign it to a variable:
pairs = [byte[i:i+2] for i in xrange(0, len(byte), 2)]
Now, you can use another comprehension (or an explicit for
statement) to add the 0x
to each one:
c_pairs = ['0x' + pair for pair in pairs]
And then you can join them all up with commas:
c_initializer = ', '.join(c_pairs)
It's worth noting that you could just as easily use generator expressions instead of list comprehensions here, saving a bit of memory. And you can merge the separate steps together, too.* For example:
c_initializer = ', '.join('0x' + byte[i:i+2] for i in xrange(0, len(byte), 2))
You probably also want to stick some braces around the string, and write it out somewhere, but I'll leave that part as an exercise for the reader; the fact that you know how to use file.read
and str.replace
means you can probably handle these similar tasks pretty easily.
* Although if you merge all the steps together, as I did, replacing the single list comprehension with a generator expression doesn't actually add any benefit anymore, because if you give str.join
an iterator it just makes a list out of it anyway.
Upvotes: 2