Python Forum
string to hex and back again - 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: string to hex and back again (/thread-3058.html)

Pages: 1 2


RE: string to hex and back again - Ofnuts - Apr-28-2017

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)



RE: string to hex and back again - Skaperen - Apr-28-2017

/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.


RE: string to hex and back again - volcano63 - Apr-28-2017

(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 ?


RE: string to hex and back again - wavic - Apr-28-2017

So you don't really care what is printed as long as the value remains untouched?


RE: string to hex and back again - Skaperen - Apr-29-2017

(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.