473,587 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting to ascii?

Thekid
145 New Member
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 21595
ilikepython
844 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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
WhiteRider
37 New Member
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 Recognized Expert Contributor
Is there a way to get the number value of a character?
Yes, using the ord() function.
Oct 2 '07 #5
ghostdog74
511 Recognized Expert Contributor
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 Recognized Expert Expert
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
"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"9 5"

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
10684
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 in unicode (I am unsure of which flavour or bit depth). as I read in the file one line at a time (readlines()) it converts to ascii. Simple enough....
12
70525
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!). Could anyone help me out? Thanks Dave
4
5377
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 "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
7
2049
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 take some precious minutes to help me out. Thanks John Public Function SimpleCrypt( ByVal Text As String) As String Dim strTempChar As String, i...
5
1481
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 so i have had to try and work out how it is doing it. From what i can see it is sending the data in ASCII because if i try to use Unicode it just...
2
3538
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 entered. I know what the Ascii value of each letter, so i understood that i can transfer my string to BYTE and enter the ascii value by myself. if...
12
7741
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 assistance gratefully received. Best regards,
5
4174
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 serial ports as bytes and some of these represent strings. (Like the Packed BCD date/time stamp etc). At present I read through each Byte and convert...
6
28096
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 places. So far, I cannot find anything that works. My application gets a string of characters from an external device via the serial port. I can use the...
0
5442
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 Gridview +SqlDataSource, they are actually ASCII format, but I couldn't just convert the encoding of the page to get the proper UTF-8 format Korean...
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.