Python Forum
encoding control characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
encoding control characters
#1
has anyone seen code to encode control characters? here is my code to decode control characters. encode is Carriage Return -> '\r', decode is '\r' -> Carriage Return.
from __future__ import division, generators, print_function, with_statement
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
# -*- coding: utf-8 -*-
# file          decodecontrol.pyf
# purpose       function to decode control characters
# email         10054452614123394844460370234029112340408691
# function      decode_control
#
# purpose       decode character sequences that encode other characters like
#               control characters that are not readily entered in a plain
#               character string.
#
# 1 argument    string with encoded control characters - ascii
#
# returns       tuple (unprocessed count,decoded result string,end char)
#
# codes               ^a or \001 or \x01 = {1} Ctrl-A
#                     ^b or \002 or \x02 = {2} Ctrl-B
#               \c or ^c or \003 or \x03 = {3} Ctrl-C
#                     ^d or \004 or \x04 = {4} Ctrl-D
#                     ^e or \005 or \x05 = {5} Ctrl-E
#                     ^f or \006 or \x06 = {6} Ctrl-F
#               \a or ^g or \007 or \x07 = {7} Alarm
#               \b or ^h or \010 or \x08 = {8} Backspace
#               \t or ^i or \011 or \x09 = {9} Tab
#               \n or ^j or \012 or \x0a = {10} Newline
#               \v or ^k or \013 or \x0b = {11} Vertical Tab
#               \f or ^l or \014 or \x0c = {12} Formfeed
#               \r or ^m or \015 or \x0d = {13} Carriage Return
#               \e or ^[ or \033 or \x1b = {27} Escape
#               \\       or \134 or \x5c = {92} literal backslash
#               \^       or \136 or \x5e = {94} literal carat
#                     ^@ or \0   or \x00 = {0} raw binary zero
#               \[0-3][0-7][0-7]         converts octal to raw
#               \x[0-9a-fA-F][0-9a-fA-F] converts cetal to raw
#               \o[0-7][0-7][0-7]        converts octal to raw
#               \d[0-2][0-9][0-9]        converts decimal to raw
#
# note          only 8 bits is used for bytes encoded with the \d sequence
#               for any value given in the 3 digits it interprets.
#-------#-------#-------#-------#-------#-------#-------#-------#-------#-------#
def decode_control(istr):
    code = {'a':7,'b':8,'c':3,'t':9,'n':10,'v':11,'f':12,'r':13,'z':26,'e':27,'\\':92,'^':94}
    base = {'d':(3,10),'o':(3,8),'x':(2,16)}
    l = len(istr)
    ostr = ''
    while istr:
        c = istr[0]
        istr = istr[1:]
        l = len(istr)
        if c == '^':
            if l < 1:
                return l,ostr,c
            c,istr,l = chr(ord(istr[0])%32),istr[1:],l-1
        elif c == '\\':
            if l < 1:
                return l,ostr,c
            c,istr,l = istr[0].lower(),istr[1:],l-1
            if c in code:
                c = chr(code[c])
            elif c in ('0','1','2','3'):
                c = None
                try:
                    for x in range(3,6):
                        c = int(istr[2:x],8)
                except ValueError:
                    if c is None:
                        return l,ostr,c
            elif c in base:
                if l < 2:
                    return l,ostr,c
                d,b = base[c]
                try:
                    digits = istr[1:2+d]
                    c = int(istr[1:2+d],b) & 255
                except ValueError:
                    return l,ostr,c
            else:
                return l,ostr,c
        ostr += c
    return l,ostr,None
# EOF
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
encoding control characters - by Skaperen - Jan-25-2018, 04:02 AM
RE: encoding control characters - by sparkz_alot - Jan-25-2018, 02:30 PM
RE: encoding control characters - by Skaperen - Jan-26-2018, 07:03 AM
RE: encoding control characters - by sparkz_alot - Jan-26-2018, 03:08 PM
RE: encoding control characters - by Skaperen - Jan-27-2018, 04:03 AM
RE: encoding control characters - by sparkz_alot - Jan-27-2018, 02:18 PM
RE: encoding control characters - by Skaperen - Jan-28-2018, 02:30 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020