How do I convert characters into integers?

M

Markus Zeindl

Hello,

I want to write a simple encrypter, but I've got a problem:
How can I convert characters into integers?

I have got a string from the user, for example "Hi!".
Now I get every character with a loop:
<code>
buffer = ""
for i in range(len(message)):
ch = message[i-1:i]
...
</code>
The character is saved in ch.
Here is the problem. I got a string with one character and I
want his ascii representain, for "a" -> 97
but I want to calculate like iCh = iCh+3 or something else.
The next step is how I convert it back to an char and append it
to the buffer, but that' no problem.

thx
 
M

Markus Zeindl

Grant said:
I want to write a simple encrypter, but I've got a problem:
How can I convert characters into integers?


$ python
Python 2.3.4 (#2, Aug 19 2004, 15:49:40)
[GCC 3.4.1 (Mandrakelinux (Alpha 3.4.1-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.

65
thank you!
 
G

Grant Edwards

I want to write a simple encrypter, but I've got a problem:
How can I convert characters into integers?

$ python
Python 2.3.4 (#2, Aug 19 2004, 15:49:40)
[GCC 3.4.1 (Mandrakelinux (Alpha 3.4.1-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.65
 
P

Paul Rubin

Markus Zeindl said:
Now I get every character with a loop:
<code>
buffer = ""
for i in range(len(message)):
ch = message[i-1:i]

You mean
ch = message
what you have does the wrong thing when i = 0.
Here is the problem. I got a string with one character and I
want his ascii representain, for "a" -> 97
but I want to calculate like iCh = iCh+3 or something else.

iCh = ord(ch)
The next step is how I convert it back to an char and append it
to the buffer, but that' no problem.

ch = chr(iCh)
 
K

Kent Johnson

Markus said:
I have got a string from the user, for example "Hi!".
Now I get every character with a loop:
<code>
buffer = ""
for i in range(len(message)):
ch = message[i-1:i]

for ch in message:
...

is simpler and more idiomatic.

Kent
 
S

Scott David Daniels

Markus said:
Hello,

I want to write a simple encrypter, but I've got a problem:
How can I convert characters into integers?....


Check this out, you'll like it even more than ord/chr:
import array

def mangle(message):
a = array.array('B')
a.fromstring(message)
for i in range(len(a)):
a += 1
return a.tostring()

print mangle('Hello, Harry')


-Scott David Daniels
(e-mail address removed)
 
A

Adam DePrince

Markus Zeindl said:
Now I get every character with a loop:
<code>
buffer = ""
for i in range(len(message)):
ch = message[i-1:i]

You mean
ch = message
what you have does the wrong thing when i = 0.
Here is the problem. I got a string with one character and I
want his ascii representain, for "a" -> 97
but I want to calculate like iCh = iCh+3 or something else.

iCh = ord(ch)
The next step is how I convert it back to an char and append it
to the buffer, but that' no problem.

ch = chr(iCh)



for i in range( len( message )):
ch = message[ i ] # don't use message[i-1:i], yech!

Strings are also sequences in python. You might find this weird, but
....

"anystring"[0] == "anysring"[0][0][0][0][0]
is always true. Taking an element returns the character as a string of
length 1.

Think in terms of maping here. Don't say ...
for ...

Think ...

message = map( ord, message )

or

message = [chr( (ord( x ) + 3 )%256) for x in message]





Adam DePrince
 
T

tenax.raccoon

There's another option that I haven't seen yet as well, where you use
the translation method for strings.

<code>
# Build a list where each entry is a character whose value is 3 higher
than its index.
charList = [ chr((x+3) % 256) for x in range(256) ]
transform = ''.join(charList) # Change the list to a string, which the
tranlate method requires

print "Hello there!".translate(transform)
</code>

The translate method can be very useful, although it can only translate
on a character-by-character basis.

--Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top