Converting to ascii?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Converting to ascii?

    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....
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Thekid
    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().

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by Thekid
      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.[code=Python]>>> for item in [96,114,101,106, 60,27]:
      ... print chr(item)
      ...
      `
      r
      e
      j
      <
      
      >>> [/code]

      Comment

      • WhiteRider
        New Member
        • Sep 2007
        • 37

        #4
        Originally posted by bvdet
        Use chr() to convert an integer in the range 0 to 255 to the equivalent ascii character.[code=Python]>>> for item in [96,114,101,106, 60,27]:
        ... print chr(item)
        ...
        `
        r
        e
        j
        <
        
        >>> [/code]
        Is there a way to get the number value of a character?

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by WhiteRider
          Is there a way to get the number value of a character?
          Yes, using the ord() function.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            another way
            Code:
            ''.join([ "%c" % i for i in [96,114,41,42] ])

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by ghostdog74
              another way
              Code:
              ''.join([ "%c" % i for i in [96,114,41,42] ])
              Or, slightly more efficient (no temporary lists) using a generator and a tuple:[CODE=python]''.join("%c" % i for i in (96,114,41,42))[/CODE]

              Comment

              • Thekid
                New Member
                • Feb 2007
                • 145

                #8
                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.

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by Thekid
                  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.
                  [code=Python]s = '65$65$85$86$80 $67$87$94$90$68 $88$70$57$80$10 7$'

                  print ''.join([chr(int(i)) for i in s.split('$') if i != ''])[/code]

                  >>> AAUVPCW^ZDXF9Pk
                  >>>

                  I do not understand what you want to do with the 'shift' number.

                  Comment

                  • Thekid
                    New Member
                    • Feb 2007
                    • 145

                    #10
                    "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"

                    Code:
                    url = 'http://www.websitehere.php'
                    strSession = 'PHPSESSID=<cookiehere>' 
                    dicHeaders = {'COOKIE': strSession}
                    req = urllib2.Request(url, None, dicHeaders)
                    f = urllib2.urlopen(req)
                    strContext = f.read()
                    I think the shift is either to subtract or add to the ordinals of the characters, if that makes any sense........
                    Code:
                    .join(chr(ord(c)15) for c in original_string)

                    Comment

                    Working...