# # https://scz.617.cn/python/200604120942.txt # # ByteArray( '\\0\\t\\r\\n\\x4\\x41\\xG\\' ).hexdump() # ByteArray( '\\0\\t\\r\\n\\x4\\x41\\xG\\' ).dosomething().hexdump() # # byteArray [ 19 bytes ] -> 16 bytes per line # 00000000 5C 30 5C 74 5C 72 5C 6E-5C 78 34 5C 78 34 31 5C \0\t\r\n\x4\x41\ # 00000010 78 47 5C xG\ # byteArray [ 10 bytes ] -> 16 bytes per line # 00000000 00 09 0D 0A 04 41 5C 78-47 5C .....A\xG\ # # # 2006-04-12 09:42 scz # # 处理\\、\xH、\xHH、\r、\n、\t、\0这几种情形,形参、返回值均为字符串。 # def dosomething ( str ) : xxx = '' i = 0 while i < len( str ) : # # Python里字符不再自动等于字节,是两种数据类型。 # if '\\' != str[i] : xxx += str[i] else : # # Python的字符串没有结尾的NUL字符,务必注意这点 # if i + 1 >= len( str ) : # # 最后一个\ # xxx += str[i] elif '\\' == str[i+1] : # # 出现了两个\ # xxx += '\\' i += 1 elif 'r' == str[i+1] : # # 出现了\r # xxx += '\r' i += 1 elif 'n' == str[i+1] : # # 出现了\n # xxx += '\n' i += 1 elif 't' == str[i+1] : # # 出现了\t # xxx += '\t' i += 1 elif '0' == str[i+1] : # # 出现了\0 # xxx += '\0' i += 1 elif 'x' == str[i+1] : # # 出现了\x # if ( str[i+2] >= '0' and str[i+2] <= '9' ) or ( str[i+2] >= 'a' and str[i+2] <= 'f' ) or ( str[i+2] >= 'A' and str[i+2] <= 'F' ) : i += 2 tmp = str[i] if ( str[i+1] >= '0' and str[i+1] <= '9' ) or ( str[i+1] >= 'a' and str[i+1] <= 'f' ) or ( str[i+1] >= 'A' and str[i+1] <= 'F' ) : i += 1 tmp += str[i] else : tmp = '0' + tmp # # 按16进制串处理 # xxx += tmp.decode( 'hex_codec' ) else : xxx += str[i] else : # # 无需转义 # xxx += str[i] # # end of switch # # # end of else # i += 1 # # end of while # return( xxx ) # # end of dosomething #