1

I am currently trying to find a way to convert any sort of text to a number, so that it can later be converted back to text. So something like this:

text = "some string"
number = somefunction(text)

text = someotherfunction(number)
print(text) #output "some string"
8
  • 5
    And your question is...? and your own trials consisted in...? Commented Sep 25, 2015 at 23:30
  • What are you doing with this number? What's the point of this? If the string can be long, you'll need to have really large numbers, since the number needs as many bits as the original number of bits in the string. Commented Sep 25, 2015 at 23:33
  • @MASL My question is if there is such a thing, i tried ascii values but those only worked for individual letters. Besides I could not find anything on google since every result gave me the question of how to turn numeric strings into integers. Commented Sep 25, 2015 at 23:37
  • @Barmar I want to create a little encryption programme and for that i need the string as a number to perform calculations, but I also need to be able to convert the number back to the original string. Commented Sep 25, 2015 at 23:41
  • Operate on the individual characters, not the whole string. Commented Sep 25, 2015 at 23:42

2 Answers 2

7

If you're using Python 3, it's pretty easy. First, convert the str to bytes in a chosen encoding (utf-8 is usually appropriate), then use int.from_bytes to convert to an int:

number = int.from_bytes(mystring.encode('utf-8'), 'little')

Converting back is slightly trickier (and will lose trailing NUL bytes unless you've stored how long the resulting string should be somewhere else; if you switch to 'big' endianness, you lose leading NUL bytes instead of trailing):

recoveredstring = number.to_bytes((number.bit_length() + 7) // 8, 'little').decode('utf-8')

You can do something similar in Python 2, but it's less efficient/direct:

import binascii
number = int(binascii.hexlify(mystring.encode('utf-8')), 16)

hx = '%x' % number
hx = hx.zfill(len(hx) + (len(hx) & 1))  # Make even length hex nibbles
recoveredstring = binascii.unhexlify(hx).decode('utf-8')

That's equivalent to the 'big' endian approach in Python 3; reversing the intermediate bytes as you go in each direction would get the 'little' effect.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the ASCII values to do this:

ASCII to int:

ord('a')  # = 97

Back to a string:

str(unichr(97))  # = 'a'

From there you could iterate over the string one character at a time and store these in another string. Assuming you are using standard ASCII characters, you would need to zero pad the numbers (because some are two digits and some three) like so:

s = 'My string'
number_string = ''

for c in s:
    number_string += str(ord(c)).zfill(3)

To decode this, you will read the new string three characters at a time and decode them into a new string.

This assumes a few things:

  • all characters can be represented by ASCII (you could use Unicode code points if not)
  • you are storing the numeric value as a string, not as an actual int type (not a big deal in Python—saves you from having to deal with maximum values for int on different systems)
  • you absolutely must have a numeric value, i.e. some kind of hexadecimal representation (which could be converted into an int) and cryptographic algorithms won't work
  • we're not talking about GB+ of text that needs to be converted in this manner

8 Comments

That will work for a single character, how do you extend it to a long string?
I was hoping the OP understood how to iterate a string at least, but added a good example for good measure
He wants a single number, not a list. After all, a string is just an array of character codes internally.
Since i need to do calculations on the number I would prefer a method where a single number can be returned, since otherwise i would have to store extra data for converting the number back into the original list
Thanks I didn't know there was such a thing as zfill. I'll leave the question open for a little bit and if nothing else comes in i'll accept your answer. Thanks again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.