difficult string conversion need help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: difficult string conversion need help (/thread-38804.html) |
difficult string conversion need help - Pir8Radio - Nov-27-2022 difficult for me anyway... I have a long hex little endian string... within this string are 9 sets of 16 characters. this string can vary from 3 sets of 16 to many sets of 16 characters... I only need the first 16 characters of each set of 3. I need to convert these 16 characters to decimal from hex little endian.. Example here is a string: 2692692A7BAE0E47430678A4B4382D13F032AE1C509ED523EF747FF795260E917A0DA53F07140C408DA814235B0A2BDD3EBA832D8F2C82FE0AD1B0FD092515A475C04BC130D77E3C that string broken out looks like: 2692692A7BAE0E47 430678A4B4382D13 F032AE1C509ED523 EF747FF795260E91 7A0DA53F07140C40 8DA814235B0A2BDD 3EBA832D8F2C82FE 0AD1B0FD092515A4 75C04BC130D77E3C The only blocks i care about is the first, skip two, next, skip two, next and so on.. so i need the ones in bold.. and i need to convert them to dec.. 2692692A7BAE0E47 = 5120221670382604838 EF747FF795260E91 = 10452334210717807855 3EBA832D8F2C82FE = 18339269626061634110 again this string can contain one set of three or 30 sets of 3 and i only need the first set of each Help would be appreciated lol.. im loosing it over here.. RE: difficult string conversion need help - Gribouillis - Nov-27-2022 It is easy to extract the substrings, but where do you get the integer values from? >>> s = '2692692A7BAE0E47430678A4B4382D13F032AE1C509ED523EF747FF795260E917A0DA53F07140C408DA814235B0A2BDD3EBA832D8F2C82FE0AD1B0FD092515A475C04BC130D77E3C' >>> for x in (s[k:k+16] for k in range(0, len(s), 3*16)): ... print(x) ... 2692692A7BAE0E47 EF747FF795260E91 3EBA832D8F2C82FE >>>Edit: I think I've found a way >>> from binascii import a2b_hex >>> import struct >>> >>> s = '2692692A7BAE0E47430678A4B4382D13F032AE1C509ED523EF747FF795260E917A0DA53F07140C408DA814235B0A2BDD3EBA832D8F2C82FE0AD1B0FD092515A475C04BC130D77E3C' >>> >>> for x in (s[k:k+16] for k in range(0, len(s), 3*16)): ... z = a2b_hex(x) ... i = struct.unpack('<Q', z)[0] ... print(x, i) ... 2692692A7BAE0E47 5120221670382604838 EF747FF795260E91 10452334210717807855 3EBA832D8F2C82FE 18339269626061634110 RE: difficult string conversion need help - Pir8Radio - Nov-27-2022 you suck... lol (joking) I'm just jealous I racked my brain, I still have a lot of learning to do... thank you so much for turning this around so quick.. and in such efficient code.. i really appreciate it! Let me get this into the rest of my project and see how it works!!!! thank you again!!!!!!!!!! RE: difficult string conversion need help - Gribouillis - Nov-27-2022 Thanks, that was easy. According to the documentation, one can use bytes.fromhex() instead of a2b_hex(). It's a little prettier import struct s = '2692692A7BAE0E47430678A4B4382D13F032AE1C509ED523EF747FF795260E917A0DA53F07140C408DA814235B0A2BDD3EBA832D8F2C82FE0AD1B0FD092515A475C04BC130D77E3C' for x in (s[k:k+16] for k in range(0, len(s), 3*16)): i = struct.unpack('<Q', bytes.fromhex(x))[0] print(x, i) |