472,146 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Converting to ascii?

Thekid
145 100+
How do you go about converting something to ASCII using python? I have an example below of some randomly generated text that would need converted:

96,114,101,106,60,
Shift: 27

I don't know where to begin with this....
Oct 1 '07 #1
9 21468
ilikepython
844 Expert 512MB
How do you go about converting something to ASCII using python? I have an example below of some randomly generated text that would need converted:

96,114,101,106,60,
Shift: 27

I don't know where to begin with this....
Try the built in functions ord() and chr().
Oct 1 '07 #2
bvdet
2,851 Expert Mod 2GB
How do you go about converting something to ASCII using python? I have an example below of some randomly generated text that would need converted:

96,114,101,106,60,
Shift: 27

I don't know where to begin with this....
Use chr() to convert an integer in the range 0 to 255 to the equivalent ascii character.
Expand|Select|Wrap|Line Numbers
  1. >>> for item in [96,114,101,106,60,27]:
  2. ...     print chr(item)
  3. ...     
  4. `
  5. r
  6. e
  7. j
  8. <
  9. 
  10. >>> 
Oct 1 '07 #3
Use chr() to convert an integer in the range 0 to 255 to the equivalent ascii character.
Expand|Select|Wrap|Line Numbers
  1. >>> for item in [96,114,101,106,60,27]:
  2. ...     print chr(item)
  3. ...     
  4. `
  5. r
  6. e
  7. j
  8. <
  9. 
  10. >>> 
Is there a way to get the number value of a character?
Oct 2 '07 #4
ilikepython
844 Expert 512MB
Is there a way to get the number value of a character?
Yes, using the ord() function.
Oct 2 '07 #5
ghostdog74
511 Expert 256MB
another way
Expand|Select|Wrap|Line Numbers
  1. ''.join([ "%c" % i for i in [96,114,41,42] ])
  2.  
Oct 2 '07 #6
bartonc
6,596 Expert 4TB
another way
Expand|Select|Wrap|Line Numbers
  1. ''.join([ "%c" % i for i in [96,114,41,42] ])
  2.  
Or, slightly more efficient (no temporary lists) using a generator and a tuple:
Expand|Select|Wrap|Line Numbers
  1. ''.join("%c" % i for i in (96,114,41,42))
Oct 2 '07 #7
Thekid
145 100+
Thank you all for your replies, but what about something like this:

Generated String: 65$65$85$86$80$67$87$94$90$68$88$70$57$80$107$
Shift: 20

I want to take that string and print out the decoded ascii, also using shifts. I
believe the shifts are from 0-30?? chr() and ord() don't seem to work with symbols in the string and the strings are randomly generated so they vary each time.
Oct 2 '07 #8
bvdet
2,851 Expert Mod 2GB
Thank you all for your replies, but what about something like this:

Generated String: 65$65$85$86$80$67$87$94$90$68$88$70$57$80$107$
Shift: 20

I want to take that string and print out the decoded ascii, also using shifts. I
believe the shifts are from 0-30?? chr() and ord() don't seem to work with symbols in the string and the strings are randomly generated so they vary each time.
Expand|Select|Wrap|Line Numbers
  1. s = '65$65$85$86$80$67$87$94$90$68$88$70$57$80$107$'
  2.  
  3. print ''.join([chr(int(i)) for i in s.split('$') if i != ''])
>>> AAUVPCW^ZDXF9Pk
>>>

I do not understand what you want to do with the 'shift' number.
Oct 2 '07 #9
Thekid
145 100+
"This string was randomly generated. It will not be recognizable text. You have 3 seconds to take the information from the website, and apply that to your algorithm.

Generated String: 73"51"92"51"92"81"66"87"71"48"61"62"76"87"95"

Shift: 15"

Expand|Select|Wrap|Line Numbers
  1.  
  2. url = 'http://www.websitehere.php'
  3. strSession = 'PHPSESSID=<cookiehere>' 
  4. dicHeaders = {'COOKIE': strSession}
  5. req = urllib2.Request(url, None, dicHeaders)
  6. f = urllib2.urlopen(req)
  7. strContext = f.read()
  8.  
I think the shift is either to subtract or add to the ordinals of the characters, if that makes any sense........
Expand|Select|Wrap|Line Numbers
  1. .join(chr(ord(c)15) for c in original_string)
  2.  
Oct 23 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Peter Wilkinson | last post: by
12 posts views Thread by David Williams | last post: by
4 posts views Thread by Prabhu | last post: by
7 posts views Thread by jj | last post: by
5 posts views Thread by scott | last post: by
6 posts views Thread by davetelling | last post: by
reply views Thread by Alci | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.