编码转换

建立日期: 2020/05/03

修订日期: None

相关软件信息:

Win 10 Python 3.7.6

说明: 本文请随意引用或更改,只须标示出处及作者,作者不保证内容绝对正确无误,如造成任何后果,请自行负责.

前言

整理一下有关各类 strbytes 的编码转换, 一下子想不到太多, 不一定齐全.

介绍

以下内容不细说其他各项参数, quote以及unquote 来自urllib.parse

  1. str.encode(encoding='utf-8')

    按指定的编码转换 str 为 bytes

  2. bytes.decode(encoding='utf-8')

    按指定的编码转换 bytes 为 str

  3. bytes.hex()

    转换 byte 为十六进位数字的 str

  4. bytes.fromhex(str)

    转换十六进位的字符串为 bytes

  5. quote(str | bytes)

    转换 bytes 或 str 为 url 编码方式的 str

  6. unquote(str)

    转换 url 编码方式的 str 为 unicode str

  7. 编码 'latin-1'

    byte 与 char 的转换

代码

六种不同格式的 str 或 bytes 之间的转换.

from urllib.parse import quote, unquote

def test(a, b):
    global flag
    if a != b:
        print(f'{a} != {b}')
        flag = False

String_Unicode = '珠海市2020年'
String_Hexcode = '\xe7\x8f\xa0\xe6\xb5\xb7\xe5\xb8\x822020\xe5\xb9\xb4'
String_Bytecode = 'e78fa0e6b5b7e5b88232303230e5b9b4'
String_Urlcode = '%E7%8F%A0%E6%B5%B7%E5%B8%822020%E5%B9%B4'

Byte_Unicode = b'\xe7\x8f\xa0\xe6\xb5\xb7\xe5\xb8\x822020\xe5\xb9\xb4'
Byte_Hexcode = b'e78fa0e6b5b7e5b88232303230e5b9b4'

flag = True

# String_Unicode
test(String_Unicode.encode().decode('latin-1'), String_Hexcode)
test(String_Unicode.encode().hex(), String_Bytecode)
test(quote(String_Unicode), String_Urlcode)
test(String_Unicode.encode(), Byte_Unicode)
test(String_Unicode.encode().hex().encode(), Byte_Hexcode)

# String_Hexcode
test(String_Hexcode.encode('latin-1').decode(), String_Unicode)
test(String_Hexcode.encode('latin-1').hex(), String_Bytecode)
test(quote(String_Hexcode.encode('latin-1')), String_Urlcode)
test(String_Hexcode.encode('latin-1'), Byte_Unicode)
test(String_Hexcode.encode('latin-1').hex().encode(), Byte_Hexcode)

# String_Bytecode
test(bytes.fromhex(String_Bytecode).decode(), String_Unicode)
test(bytes.fromhex(String_Bytecode).decode('latin-1'), String_Hexcode)
test(quote(bytes.fromhex(String_Bytecode)), String_Urlcode)
test(bytes.fromhex(String_Bytecode), Byte_Unicode)
test(bytes.fromhex(String_Bytecode).hex().encode('latin-1'), Byte_Hexcode)

# String_Urlcode
test(unquote(String_Urlcode), String_Unicode)
test(unquote(String_Urlcode).encode().decode('latin-1'), String_Hexcode)
test(unquote(String_Urlcode).encode().hex(), String_Bytecode)
test(unquote(String_Urlcode).encode(), Byte_Unicode)
test(unquote(String_Urlcode).encode().hex().encode(), Byte_Hexcode)

# Byte_Unicode
test(Byte_Unicode.decode(), String_Unicode)
test(Byte_Unicode.decode('latin-1'), String_Hexcode)
test(Byte_Unicode.hex(), String_Bytecode)
test(quote(Byte_Unicode), String_Urlcode)
test(Byte_Unicode.hex().encode(), Byte_Hexcode)

# Byte_Hexcode
test(bytes.fromhex(Byte_Hexcode.decode('latin-1')).decode(), String_Unicode)
test(bytes.fromhex(Byte_Hexcode.decode()).decode('latin-1'), String_Hexcode)
test(Byte_Hexcode.decode(), String_Bytecode)
test(quote(bytes.fromhex(Byte_Hexcode.decode('latin-1'))), String_Urlcode)
test(bytes.fromhex(Byte_Hexcode.decode('latin-1')), Byte_Unicode)

if not flag:
    print('All Pass !')
本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang
讨论数量: 1

感觉ascci编码最实用,可惜不支持中文

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!