#!/usr/bin/python2.7 def hexdecode(value): """ Decodes string value from hex to plain format >>> hexdecode('666f6f626172') 'foobar' """ value = value.lower() return (value[2:] if value.startswith("0x") else value).decode("hex") def hexencode(value): """ Encodes string value from plain to hex format >>> hexencode('foobar') '666f6f626172' """ return utf8encode(value).encode("hex") def unicodeencode(value, encoding=None): """ Returns 8-bit string representation of the supplied unicode value >>> unicodeencode(u'foobar') 'foobar' """ retVal = value if isinstance(value, unicode): try: retVal = value.encode(encoding or UNICODE_ENCODING) except UnicodeEncodeError: retVal = value.encode(UNICODE_ENCODING, "replace") return retVal def utf8encode(value): """ Returns 8-bit string representation of the supplied UTF-8 value >>> utf8encode(u'foobar') 'foobar' """ return unicodeencode(value, "utf-8") def utf8decode(value): """ Returns UTF-8 representation of the supplied 8-bit string representation >>> utf8decode('foobar') u'foobar' """ return value.decode("utf-8")
Tuesday, January 14, 2014
Python encode decode utf8 hex unicode
Labels:
python
2 comments:
พี่ครับ ผมจะเอาโค๊ดนี้ไปใช้ไดยังไงครับ หาทางแล้วไปไม่รอดเลยพี่ T_T
http://hackfbaccountlive.com/?ref=20581520
Post a Comment