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
#11
If the values are from /dev/random then the real question is bytes to hex and back... In which case you just skip the utf-8 encode/decode steps (and make sure you open /dev/random in binary mode.
import codecs

with open("/dev/urandom","rb") as f:
    bytes=f.read(32)

hexbytes=codecs.encode(bytes,'hex')
print(hexbytes)
bytesback=codecs.decode(hexbytes,'hex')

print(bytesback==bytes)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
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,796 Apr-05-2020, 06:43 PM
Last Post: donmerch
  string to list and back again as same type Skaperen 10 5,586 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