Convert hex to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Java and Swing

    #1

    Convert hex to string

    I have some output stored in a string that looks like..
    [color=blue][color=green]
    >> x[/color][/color]
    '\x01\xee\x1eo\ xc3+\x8b\x83\xf ad\xf6E\xaa\x0e a/I\x96\x83\xf5G\ xa3\rQ\xfcH\xee \r'


    According to, http://docs.python.org/lib/typesseq-strings.html, this is
    "Unsigned Hexidecimal (lowercase)". How can I get this into normal
    Ascii so that I can read it?

    thanks.

  • jepler@unpythonic.net

    #2
    Re: Convert hex to string

    it *is* a string.

    because most of the characters are not printable, they're presented using hex
    notation when you repr() the string. That's what entering an expression inthe
    interactive interpreter does (except that it never prints 'None').

    If you really want to write whatever those bytes are, then do something like[color=blue][color=green][color=darkred]
    >>> print x[/color][/color][/color]
    or [color=blue][color=green][color=darkred]
    >>> sys.stdout.writ e(x)[/color][/color][/color]

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD4DBQFDQZT8Jd0 1MZaTXX0RAm4QAJ 9QKsJlGbPpSkChL PQfKvbMUdS4QQCY uMuK
    vcQ5hyK6Vj62SFD HS/gKQA==
    =7AYM
    -----END PGP SIGNATURE-----

    Comment

    • Larry Bates

      #3
      Re: Convert hex to string

      It contains non-printing and non-ASCII characters so it can't be
      printed lie you want. The best Python can do is to show you
      the hexadecimal of those bytes (as it has done). Printable ASCII
      characters lie in the range 32 decimal (20 hex) to 125 decimal
      (7F hex). Those characters in that range are shown properly.

      Example: There is no printable ASCII character for the first
      character which is \x01. My ASCII table shows that this
      character represents an ASCII SOH (start of heading) character.

      -Larry Bates

      Java and Swing wrote:[color=blue]
      > I have some output stored in a string that looks like..
      >
      >[color=green][color=darkred]
      >>>x[/color][/color]
      >
      > '\x01\xee\x1eo\ xc3+\x8b\x83\xf ad\xf6E\xaa\x0e a/I\x96\x83\xf5G\ xa3\rQ\xfcH\xee \r'
      >
      >
      > According to, http://docs.python.org/lib/typesseq-strings.html, this is
      > "Unsigned Hexidecimal (lowercase)". How can I get this into normal
      > Ascii so that I can read it?
      >
      > thanks.
      >[/color]

      Comment

      • Magnus Lycka

        #4
        Re: Convert hex to string

        Java and Swing wrote:[color=blue]
        > I have some output stored in a string that looks like..
        >
        >[color=green][color=darkred]
        >>>x[/color][/color]
        >
        > '\x01\xee\x1eo\ xc3+\x8b\x83\xf ad\xf6E\xaa\x0e a/I\x96\x83\xf5G\ xa3\rQ\xfcH\xee \r'
        >
        >
        > According to, http://docs.python.org/lib/typesseq-strings.html, this is
        > "Unsigned Hexidecimal (lowercase)". How can I get this into normal
        > Ascii so that I can read it?[/color]

        What did you expect this string to print? Where did it come from?
        Viewed as ASCII it's mainly non-printables, with o, +, d, E, a/I,
        G, Q and H stuffed in, but I'm pretty sure it isn't 8 bit text
        in any normal encoding. The only reason to suspect that this might
        be text after all, is that it ends with a carriage return.

        Converting your string to numbers, we get...
        1 238 30 111(o) 195 43(+) 139 131 250 100(d) 246 69(E) 170 14 97(a)
        47(/) 73(I) 150 131 245 71(G) 163 13 81 (Q) 252 72(H) 238 13.

        You can find the definition of ASCII via Google.

        Comment

        Working...