How to print non-printable chars??

Discussion in 'Python' started by Julio Cesar Rodriguez Cruz, Aug 13, 2011.

  1. Hi all,
    If I open an .exe file in any text editor I get lot of odd chars,
    what I want is to know how to output those chars if I have the hexadecimal
    code. I found out how to do the reverse process with the quopri module,

    i.e.:
    >>> import quopri
    >>> quopri.encodestring('ñè')

    '=F1=E8=18'
    >>> quopri.decodestring('=F1=E8=18')

    '\xf1\xe8\x18'

    but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè'

    any tips?
    thanks
    Julio Cesar
    Julio Cesar Rodriguez Cruz, Aug 13, 2011
    #1
    1. Advertising

  2. Julio Cesar Rodriguez Cruz

    Nobody Guest

    On Sat, 13 Aug 2011 00:59:42 -0400, Julio Cesar Rodriguez Cruz wrote:

    > Hi all,
    > If I open an .exe file in any text editor I get lot of odd chars,
    > what I want is to know how to output those chars if I have the hexadecimal
    > code. I found out how to do the reverse process with the quopri module,
    >
    > i.e.:
    >>>> import quopri
    >>>> quopri.encodestring('ñè')

    > '=F1=E8=18'
    >>>> quopri.decodestring('=F1=E8=18')

    > '\xf1\xe8\x18'
    >
    > but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè'


    print(quopri.decodestring('=F1=E8=18'))
    or:
    sys.stdout.write(quopri.decodestring('=F1=E8=18'))

    If you type an expression into the interactive Python interpreter, the
    result is converted to a string using repr(); for strings, this converts
    8-bit characters to their hexadecimal escape sequences, so that the result
    only uses ASCII.

    OTOH, the print statement converts values to strings using str(); for
    strings, this is an identity operation (i.e. it returns the original
    string untouched). Similarly, the .write() method of file objects uses str().
    Nobody, Aug 13, 2011
    #2
    1. Advertising

  3. Julio Cesar Rodriguez Cruz

    Julio Cesar Guest

    On Aug 13, 1:22 am, Nobody <> wrote:
    > On Sat, 13 Aug 2011 00:59:42 -0400, Julio Cesar Rodriguez Cruz wrote:
    >
    > > Hi all,
    > > If I open an .exe file in any text editor I get lot of odd chars,
    > > what I want is to know how to output those chars if I have the hexadecimal
    > > code. I found out how to do the reverse process with the quopri module,

    >
    > > i.e.:
    > >>>> import quopri
    > >>>> quopri.encodestring('ñè ')

    > > '=F1=E8=18'
    > >>>> quopri.decodestring('=F1=E8=18')

    > > '\xf1\xe8\x18'

    >
    > > but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè '

    >
    >         print(quopri.decodestring('=F1=E8=18'))
    > or:
    >         sys.stdout.write(quopri.decodestring('=F1=E8=18'))
    >
    > If you type an expression into the interactive Python interpreter, the
    > result is converted to a string using repr(); for strings, this converts
    > 8-bit characters to their hexadecimal escape sequences, so that the result
    > only uses ASCII.
    >
    > OTOH, the print statement converts values to strings using str(); for
    > strings, this is an identity operation (i.e. it returns the original
    > string untouched). Similarly, the .write() method of file objects uses str().


    It just works!! thanks a lot and also for the explanations ;)
    Cheers
    Julio Cesar
    Julio Cesar, Aug 13, 2011
    #3
  4. Julio Cesar Rodriguez Cruz

    coldpizza Guest

    On Aug 13, 7:59 am, Julio Cesar Rodriguez Cruz
    <> wrote:
    > Hi all,
    > If I open an .exe file in any text editor I get lot of odd chars,
    > what I want is to know how to output those chars if I have the hexadecimal
    > code. I found out how to do the reverse process with the quopri module,
    >
    > i.e.:>>> import quopri
    > >>> quopri.encodestring('ñè ')

    > '=F1=E8=18'
    > >>> quopri.decodestring('=F1=E8=18')

    >
    > '\xf1\xe8\x18'
    >
    > but how to do the reverse? ...gived '\xf1\xe8\x18', print 'ñè '
    >
    > any tips?
    > thanks
    > Julio Cesar


    In a web/html environment or in broken ascii-only consoles like the
    one on windows, I use the following hack:

    print your_unicode_string.encode('us-ascii','xmlcharrefreplace')

    This will print unicode chars using pure ASCII symbols which will
    display correctly in a web browser and are more readable in a console
    than unicode escapes.
    coldpizza, Aug 18, 2011
    #4
  5. Julio Cesar Rodriguez Cruz

    jmfauth Guest

    On 18 août, 22:44, coldpizza <> wrote:
    >
    >
    > ...
    >
    > In a web/html environment or in broken ascii-only consoles like the
    > one on windows ...


    C:\Users\Jean-Michel>echo 'Cet œuf de Lætitia coûte un €uro'
    'Cet œuf de Lætitia coûte un €uro'

    C:\Users\Jean-Michel>c:\Python27\python
    Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print 'Cet œuf de Lætitia coûte un €uro'

    Cet œuf de Lætitia coûte un €uro
    >>> import sys
    >>> u = unicode('Cet œuf de Lætitia coûte un €uro', sys.stdin.encoding)
    >>> print u.encode(sys.stdout.encoding)

    Cet œuf de Lætitia coûte un €uro
    >>>


    C:\Users\Jean-Michel>c:\Python32\python
    Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print('Cet œuf de Lætitia coûte un €uro')

    Cet œuf de Lætitia coûte un €uro
    >>>


    PS Cet œuf de Lætitia coûte un €uro ->
    This Lætitia's egg costs one €uro'

    PS2 "ñ" does not require special attention.

    PS3 To the original question: This not a *coding* issue, it is a
    character *representation* question.

    jmf
    jmfauth, Aug 19, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    0
    Views:
    588
  2. Daniel Alexandre
    Replies:
    2
    Views:
    545
    Sibylle Koczian
    Mar 21, 2005
  3. metaperl
    Replies:
    1
    Views:
    288
    Peter Otten
    Feb 9, 2007
  4. darren
    Replies:
    2
    Views:
    297
    darren
    May 24, 2008
  5. Alex Vinokur

    Printing non-printable characters

    Alex Vinokur, May 18, 2011, in forum: C++
    Replies:
    8
    Views:
    737
    Alf P. Steinbach /Usenet
    May 18, 2011
Loading...

Share This Page