2

I want get ansii code for my random symbol, but I don't know how to get him.

I made symbol generator:

import random

string = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def random_string(count: int):
    result = ""
    for i in range(count):
        if random.randint(0, 1):
            result += random.choice(string).upper()
        else: result += random.choice(string)
    return result

I generate string with length 1 and want get him ansii code. How can I make this?

clonerson
  • 23
  • 4

2 Answers2

2

You can use ord() function for you targets. Example:

print(ord(random_string(1))
m3r1v3
  • 362
  • 5
  • 19
2

Wrt

"I generate string with length 1 and want get him ansii code. How can I make this?"

Why generate the ANSII code for just one character? Currently, for a count of 10, your code generates strings like: funSyjVvJt which is fine.

If you want the whole thing in ASCII, change the last return line to:

return [ord(c) for c in result]

which will return values like [88, 101, 117, 122, 101, 78, 103, 120, 99, 77] which are the ASCII codes for the letters in XeuzeNgxcM.

aneroid
  • 12,983
  • 3
  • 36
  • 66