How do I convert a string of length four into a decimal value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darktemp
    New Member
    • Feb 2010
    • 25

    #1

    How do I convert a string of length four into a decimal value?

    I am trying to convert a 4 letter string (that can be of any character on the ASCII chart) into decimal values. For example:

    - bean which has a hex value of 62 65 61 6E should have a value of 6736203616366.

    - I know about ord() but that only works for single characters.

    Any help is appreciated :].
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    The ease of breaking up a string into it's component characters is one of the things that makes python awesome.

    Code:
    In [1]: s="bean"
    
    In [2]: for c in s:
       ...:     print ord(c),
       ...:     
       ...:     
    98 101 97 110

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      By the way, I can't figure out where 6736203616366 comes from?!? What is it?

      Comment

      • darktemp
        New Member
        • Feb 2010
        • 25

        #4
        From here.



        The value comes from the hex representation of the string I included with the original post.

        Oh whoops, I forgot to remove spaces. I've been exhausted of all my energy today. xD

        1650811246 is the value it should be.

        Thanks for your help, I really appreciate it :].

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Can you explain how you get the decimal value "1650811246 " from "62 65 61 6E"?

          Comment

          • darktemp
            New Member
            • Feb 2010
            • 25

            #6
            Copy and paste "6265616E" into the website http://www.easycalculation.com/hex-converter.php.

            Which converts the hexadecimal representation of "bean" into a decimal value which is 1650811246 according to the website.

            I hope I am writing this correctly.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Now I get it.
              Code:
              >>> ''.join([hex(ord(letter))[2:] for letter in 'bean'])
              '6265616e'
              >>> int('6265616e', 16)
              1650811246
              >>>

              Comment

              • darktemp
                New Member
                • Feb 2010
                • 25

                #8
                Wow, thanks it worked! There is a bit of a glitch with it though.

                07079533 should be 117937459 but it is 7836979 which in hex is 779533.

                Anyway to also include the zeros? What I am doing is reading four characters and outputting them into decimal numbers.

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  What word gives you hex values of "07 07 95 33"?

                  Comment

                  • darktemp
                    New Member
                    • Feb 2010
                    • 25

                    #10
                    Oh its not a word, the program I am making also converts special characters and other things like NUL characters also. I just used the above hex representation as a example of that.

                    I really appreciate your help :].

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      The code I posted should work for special characters. Function hex() takes an integer and returns a string.
                      Code:
                      >>> hex(4)
                      '0x4'
                      >>> hex(ord('\t'))
                      '0x9'
                      >>>

                      Comment

                      • darktemp
                        New Member
                        • Feb 2010
                        • 25

                        #12
                        Is there a way for this code to read the hex value as 07 and not 7? The example I posted up gives me a different value.

                        07 07 95 33 = 117937459
                        7 7 95 33 = 7836979

                        Comment

                        • Glenton
                          Recognized Expert Contributor
                          • Nov 2008
                          • 391

                          #13
                          Hi

                          @bvdet will probably have a better way, but here's one way.

                          Code:
                          In [12]: s="%2s" %"7"
                          
                          In [13]: s
                          Out[13]: ' 7'
                          
                          In [14]: s.replace(" ","0")
                          Out[14]: '07'

                          Comment

                          • darktemp
                            New Member
                            • Feb 2010
                            • 25

                            #14
                            Just tried this way and it outputs the same thing. Maybe I am just doing it wrong, but it seems that python doesn't see the hex value as 07 compared to 7 which changes the values when more hex values are put in place.

                            I really appreciate everyones help though :].

                            Comment

                            • Glenton
                              Recognized Expert Contributor
                              • Nov 2008
                              • 391

                              #15
                              Please post your code (using the code tags pls ;D). Difficult to debug when we can't see!

                              Anyway, you need to be clear what type python is using at each point. Using type(whatever) in the command line will give you an idea of whether it's an integer or string or whatever.

                              You also need to be clear about what, exactly, your conversion is.
                              Currently, it seems you want to convert:
                              characters -> 2-digit hex -> multi-digit hex by concatenation -> integer

                              Your characters start off as a string (I guess).
                              The 2-digit hex is a string, but the problem is it's sometimes 1-digit hex. So you could even write something explicit like (supposing hs is your hex in a string format):
                              Code:
                              if len(hs)==1:
                                  hs="0"+hs
                              At the end of this your hs is two digits. You'd need to be careful: is it possible that len(hs)==0 or len(hs)>=3. If so these cases need to be handled.

                              Good luck, and pls post your code next time if you want more specific help.

                              Comment

                              Working...