-3

I am making a code in which every character is associated with a number. To make my life easier I decided to use alphanumeric values (a=97, b=98, z=121). The first step in my code would be to get a number out of a character. For example: char = input"Write a character:" print(ord(char.lower()))

Afterwards though, I need to know the total number of alphanum characters that exist and nowhere have I found my answer...

evtoh
  • 444
  • 3
  • 10
  • 21
Benjamin Chausse
  • 1,437
  • 2
  • 10
  • 20
  • Are you using a dictionary to store those associations? – Athena Jul 19 '16 at 21:17
  • 2
    Can you please add the sample code you have written? – Dinesh Pundkar Jul 19 '16 at 21:20
  • Basically I am encrypting text and the way I created to do so requires that I know the maximum number of an alphanumeric character when I encrypt/decrypt it. I used to make my own system where a=1, ?=32 I couldn't have access to lowercaps and other characters that I didn't incorporate to my code. The easiest way I found to have every single character asigned to a number was to use user input in a while loop that changes the character to it's alphanum value. – Benjamin Chausse Jul 20 '16 at 16:16

1 Answers1

1

Your question is not very clear. Why would you need total number of alphanum characters?

thing is, that number depends on the encoding in question. If ASCII is in question then:

>>> import string
>>> len(string.letters+string.digits)

Which is something you could do by counting manually.

And this is even not really the total count, as there is a few more alpha from other languages within 0-128 ASCII range.

If unicode, well, then you will have to search for the specification to see how many of these are there. I do not even know how many alphabets are crammed into unicode or UTF-8.

If it is a question of recognizing alpha-numeric characters in a string, then Python has a nice method to do so:

>>> "A".isalnum()
>>> "0".isalnum()
>>> "[".isalnum()

So please, express yourself more clearly.

Dalen
  • 4,128
  • 1
  • 17
  • 35
  • What is the biggest number you can get out of "".isalnum() ? Basically I am encrypting text and the way my code was made, it needs to know a max value (It uses it to multiply value when encrypting). – Benjamin Chausse Jul 20 '16 at 16:31
  • The max number of "".isalnum() is 1, as it returns a boolean. You can calculate the max alnum in an encoding by iterating throughout all characters in that encoding and counting. count = 0; for x in xrange(2**16): count += unichr(x).isalnum() – Dalen Jul 20 '16 at 16:42
  • The answer to that is 1, as "".isalnum() returns a boolean. But you can count: count = 0; for x in xrange(2**16): count += unichr(x).isalnum() – Dalen Jul 20 '16 at 16:46
  • Sorry, comment 1 didn't appear so I wrote the second. Anyway, your encryption isn't something, the best would be to use xor byte by byte, your string and a key, when key is shorter than a string, then start from the beginning of a key again. This is fast and virtually uncrackable until the other person knows a key or at least part of it. – Dalen Jul 20 '16 at 16:49
  • What I meant by "what is the biggest number you can get out of "".isalnum()" is that I don't know which character has the biggest value so I just left it blank... – Benjamin Chausse Jul 20 '16 at 17:17
  • Then find out also by iterating (for ASCII): maxval = 0; for x in xrange(128): maxval = x if (x>maxval and chr(x).isalnum()) else maxval; The result of this code snippet is 122 i.e. the character 'z' in pure ASCII. For unichr() and range of 2**16, the result is 65500 which resembles God only knows what character. :D But here you are. I hope I got you right this time. – Dalen Jul 20 '16 at 21:27