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
#12
/dev/urandom is just a test source.  the need for this module is to be able to use things that expect data in a different form but with the exact same vales.   for example if i have a byte with the value 0xa0 i want to have a character with the value 0xa0.  it just needs to be the same.  that code seems to be working over the whole range of values and many combinations.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
(Apr-27-2017, 10:00 AM)Skaperen Wrote: I have put together to do lots of type conversion without any encoding:
May I humbly point out that the very fact that you read your files in binary mode makes conversions and encoding unnecessary?

And that your code does - a very hard way Wall - things that struct and array packages give you on a silver platter? If you plan on processing large quantities of data, it will be very slooooow Cry ?
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
#14
So you don't really care what is printed as long as the value remains untouched?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#15
(Apr-28-2017, 10:54 AM)wavic Wrote: So you don't really care what is printed as long as the value remains untouched?

printing is not the goal.  the test code does do printing to see test results.

the first goal is to have consistent 2 digit per unit hexadecimal conversions to/from all character and byte aggregation types.

the 2nd goal is to have type conversion between all character and byte aggregation types that is equivalent to doing the conversion first to hexadecimal and then from there to the other type using the first goal conversions.  this may be done by actually using those first goal functions or by using other faster means that has exactly identical results.

it is not intended that this involve any encoding or decoding.

yes, i do understand that 2 digits of hexadecimal means this whole thing only applies to a limited range of values.  i may end up wanting to do a larger verion, such as 4 digits of hexadecimal, which will exceed the range of values the units of some types will support.

another goal is to have code work in both python2 and python3 even if that involves testing what version of python is running.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I convert this string back to a list of integers? donmerch 6 3,739 Apr-05-2020, 06:43 PM
Last Post: donmerch
  string to list and back again as same type Skaperen 10 5,556 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