Python Forum
difficult string conversion need help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
difficult string conversion need help
#1
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..
Reply
#2
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
Reply
#3
you suck... lol (joking) I'm just jealous Angel Big Grin I racked my brain, I still have a lot of learning to do... Cry 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!!!!!!!!!!
Reply
#4
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)
Pir8Radio likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating simplex tableau pivot program easy or difficult for a beginner in Python? alex_0 2 2,538 Mar-31-2021, 03:39 AM
Last Post: Larz60+
  How to solve difficult non-linear equations? shreeniket 3 2,348 Apr-23-2020, 01:36 AM
Last Post: shreeniket
  Programming Difficult math in Python Huntern 6 4,689 Oct-17-2019, 06:32 AM
Last Post: Huntern
  Very difficult challenge for me cristfp 1 2,717 Apr-01-2019, 08:45 PM
Last Post: Yoriz
  Why is basic Twitter Connectivity So Difficult? Not sure what I'm doing wrong. Oliver 0 1,856 Mar-10-2018, 12:38 PM
Last Post: Oliver
  Python installation difficult rajamugavai 2 2,852 Feb-27-2018, 02:18 PM
Last Post: sparkz_alot
  string conversion like in source code Skaperen 4 3,824 Nov-26-2017, 03:29 AM
Last Post: Skaperen
  Projected Surface in 2D [Difficult topic] Hans_K 6 3,792 Aug-02-2017, 09:16 AM
Last Post: Hans_K

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020