3

I was trying to convert a string list to integer in python as follows:

plain = input("string >> ") string_list = list(plain) print(string_list) ASCII = [] for x in range (0, len(string_list)): ASCII.append([ord(string_list[x])]) print(ASCII) for x in range (0, len(ASCII)): print(ASCII[x]) integer_ASCII = type(int(ASCII[x])) print(integer_ASCII, end=", ") 

but I get this error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 

So is there any other way to convert a string to integer.

11
  • Please don’t use tabs when assigning variables or calling methods. It’s unnecessary white space and reduces readibility. Commented Dec 12, 2020 at 4:41
  • Can you please show what is variable "plain"? Commented Dec 12, 2020 at 4:42
  • plain is just a string input from the user like "test" Commented Dec 12, 2020 at 4:44
  • what's the result of print(string_list) Commented Dec 12, 2020 at 4:45
  • 1
    @shubhanshu-tomar you can use ord('your_letter') Commented Dec 12, 2020 at 4:49

5 Answers 5

2

instead of ascii value, your are appending a list to ASCII First, we map each string character to ASCII value and convert it into a string type. Then join with the join function.

e = 100 n = 20 text = input("string >>>") #This will return int ascii values for each character. Here you can do any arithmatic operations rsa_values = list(map(lambda x:(ord(x)**e) % n , text))#[16, 1, 5, 16] #TO get the same you can list compression also rsa_values = [ (ord(x)**e) % n for x in text] #[16, 1, 5, 16] #if you need to join them as string then use join function. string_ascii_values = "".join(chr(i) for i in ascii_values)#'\x10\x01\x05\x10' 

Update

Based on Copperfield's comment A better way to do following arithmetic operation

(ord(x)**e) % n

is

pow(ord(x), e, n)

rsa_values = [ pow(ord(x), e, n) for x in text] #[16, 1, 5, 16] 
Sign up to request clarification or add additional context in comments.

14 Comments

This clearly removes the braces and gives the output but how do I perform arithmetical operations on an individual ascii value.
I will break it step by step and post the answer soon
(ord(x)**e) % n for each element we are doing this inside the list comprehension or map function both are valid.
when doing (a**b)%m is better to use the 3 argument version of pow, pow(a,b,m) because this one does modular exponentiation which is more efficient way of calculating that
|
2

Does this resolve your error?

string_list = list("test") print(string_list) ASCII = [] for x in range(0, len(string_list)): ASCII.append([ord(string_list[x])]) print(ASCII) integer_ASCII = [] str_integer_ASCII = [] for x in range(0, len(ASCII)): integer_ASCII.append(int(ASCII[x][0])) str_integer_ASCII.append(str(ASCII[x][0])) print("\n") print("INT LIST VERSION:", integer_ASCII, type(integer_ASCII)) print("STRING LIST VERSION:", str_integer_ASCII, type(str_integer_ASCII)) print("FULL STRING VERSION: ", ' '.join(str_integer_ASCII), type(' '.join(str_integer_ASCII))) 

Hope so this information is useful to you!
Happy Coding

9 Comments

It does convert it to a multi dimensional list but still i want to convert each element in the list to an integer because I want to apply arithmetic operation on those integer values
So basically you want a list like, [116, 101, 115, 116] right?
can I actually apply arithmetic on a list because I need to encrypt the string using rsa
Well, it's a list full of integers so it be possible to to encrypt. I'm not any encryption expert but as per my perspective a list full of integers should be encrypted. So should i edit the post to make the result- [116, 101, 115, 116]?'
Of course, @shubhanshu-tomar, just like this- first create a str version of the integer_ASCII variable by appending the string value of the ascii code to after wards join them together like- str_integer_ASCII.append(str(ASCII[x][0])) then, to get the string version of them, ' '.join(str_integer_ASCII)
|
1

Just use a list comprehension that converts each string in the list into an int:

list_of_ints = [int(x) for x in string_list] 

Or if you want the char value of each char in the string (which is what you get from input() -- no need to call list() on it):

list_of_char_values = [ord(x) for x in string] 

Comments

1

Simply it is possible also. Please find about comprehension python.

>>> [ord(x) for x in 'abcde'] [97, 98, 99, 100, 101] 

1 Comment

when you do ord() it will be int, you don't need to do type cast again
-1

this seemed to work for me, you have a list inside of a list for each number.

plain = 'test' string_list = list(plain) print(string_list) ASCII = [] for x in range (0, len(string_list)): ASCII.append([ord(string_list[x])]) print(ASCII) for x in range (0, len(ASCII)): print(ASCII[x]) integer_ASCII = type(int(ASCII[x][0])) print(integer_ASCII, end=", ") 

3 Comments

Please don't post same answer multiple times I have posted the same exact solution.
I'm not downvoting @rcvaram he his clearly copying my post!
your post wasn't loaded before i posted mine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.