473,390 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 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 21580
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

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

Similar topics

12
by: Peter Wilkinson | last post by:
Hello tlistmembers, I am using the encoding function to convert unicode to ascii. At one point this code was working just fine, however, now it has broken. I am reading a text file that has is...
12
by: David Williams | last post by:
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!)....
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
7
by: jj | last post by:
It seems simple but I had a hard time converting this small function from vb.net to C#. I even tried the software that automatically converts from vb to c# , but to no avail. Please can someone...
5
by: scott | last post by:
hi all, hope some one can help me, this prob is driving me mad. im using sockets to communicate between a client and a server. I don't' have control over the client and how it sends the data...
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
12
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an unserializeable object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any...
5
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
6
by: davetelling | last post by:
I am a total newbie, trying to slog through the Visual C# Express application. I need to be able to convert a single ASCII character (can be anything from 0 to 255) to an int for use in other...
0
by: Alci | last post by:
I am getting some Korean characters data from MS SQL server. These data were submitted as UTF-8 into the database, but stored as normal varchars. So, when I getting them out of database by using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.