Python Forum
string to hex and back again
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string to hex and back again
#4
(Apr-27-2017, 04:34 AM)Skaperen Wrote: but...... can it be used with non-byte stuff?

In [7]: array.array('I', 'Skaperen'.encode())
Out[7]: array('I', [1885432659, 1852142181])

In [8]:
IMHO, struct and array are the tools to process stream data - like if you want to parse a protocol packet. Otherwise, you have to invent bells and whistle...

Here's a little more involved example with struct - split string into unequal-size fields
In [15]: struct.unpack('2h2b2s', b'Skaperen')
Out[15]: (27475, 28769, 101, 114, b'en')

In [16]:
But you have to type-cast (for the lack of better word) strings for those to work
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
string to hex and back again - by Skaperen - Apr-27-2017, 03:32 AM
RE: string to hex and back again - by volcano63 - Apr-27-2017, 04:01 AM
RE: string to hex and back again - by Skaperen - Apr-27-2017, 04:34 AM
RE: string to hex and back again - by volcano63 - Apr-27-2017, 05:13 AM
RE: string to hex and back again - by wavic - Apr-27-2017, 07:10 AM
RE: string to hex and back again - by Skaperen - Apr-27-2017, 07:26 AM
RE: string to hex and back again - by wavic - Apr-27-2017, 07:38 AM
RE: string to hex and back again - by Skaperen - Apr-27-2017, 10:00 AM
RE: string to hex and back again - by volcano63 - Apr-28-2017, 10:50 AM
RE: string to hex and back again - by Ofnuts - Apr-27-2017, 11:14 PM
RE: string to hex and back again - by wavic - Apr-28-2017, 03:51 AM
RE: string to hex and back again - by Ofnuts - Apr-28-2017, 09:31 AM
RE: string to hex and back again - by Skaperen - Apr-28-2017, 10:00 AM
RE: string to hex and back again - by wavic - Apr-28-2017, 10:54 AM
RE: string to hex and back again - by Skaperen - Apr-29-2017, 04:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I convert this string back to a list of integers? donmerch 6 3,886 Apr-05-2020, 06:43 PM
Last Post: donmerch
  string to list and back again as same type Skaperen 10 5,672 Sep-06-2018, 08:26 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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