Represent string as an integer in python

1.2k Views Asked by At

I would like to be able to represent any string as a unique integer (means every integer in the world could mean only one string, and a certain string would result constantly in the same integer).

The obvious point is, that's how the computer works, representing the string 'Hello' (for example) as a number for each character, specifically a byte (assuming ASCII encoding).

But... I would like to perform arithmetic calculations over that number (Encode it as a number using RSA).

The reason this is getting messy is because assuming I have a bit larger string 'I am an average length string' I have more characters (29 in this case), and an integer with 29 bytes could come up HUGE, maybe too much for the computer to handle (when coming up with bigger strings...?).

Basically, my question is, how could I do? I wouldn't like to use any module for RSA, it's a task I would like to implement myself.

2

There are 2 best solutions below

0
On BEST ANSWER

Here's how to turn a string into a single number. As you suspected, the number will get very large, but Python can handle integers of any arbitrary size. The usual way of working with encryption is to do individual bytes all at once, but I'm assuming this is only for a learning experience. This assumes a byte string, if you have a Unicode string you can encode to UTF-8 first.

num = 0
for ch in my_string:
    num = num << 8 + ord(ch)
1
On

I think the above code is not working as well, everytime the output is only 0, This code will definitely work:

num = 0
for ch in my_string:
    if "0" <= ch <= "9":
        num = num * 10 +(ord(ch) - ord("0"))

Here's a breakdown of the code:

  1. num = 0: Initializes a variable num to store the final numeric value.

  2. for ch in my_string:: This is a loop that iterates over each character (ch) in the string my_string.

  3. if "0" <= ch <= "9":: This conditional statement checks if the current character ch is a numeric digit. The condition "0" <= ch <= "9" is True if ch is a digit between '0' and '9' (inclusive).

  4. num = num * 10 + (ord(ch) - ord("0")): If the condition in the previous step is True, it means ch is a numeric digit. This line of code then updates the num variable by multiplying its current value by 10 (shifting digits to the left) and adding the numeric value of the current digit.