Next / Previous / Contents / TCC Help System / NM Tech homepage

51.14. asciifyChar(): Escape a non-ASCII character

Given a character, this function returns the character if it is ASCII, or the URL-encoded equivalent if it is not ASCII. Since ASCII is a seven-bit code, we consider characters with codes 0x80 and greater to be non-ASCII.

pageget.py
# - - -   a s c i i f y C h a r   - - -

def asciifyChar ( c ):
    """Escape c if it isn't ASCII, otherwise return c.

      [ c is a one-character string ->
          if ord(c) < 0x80 ->
            return c
          else ->
            return '%XX' where XX is ord(c) ]
    """
    if  ord(c) < 0x80:
        return c
    else:
        return "%%%2X" % ord(c)