encode:将字符串转成bytes类型

  1. text = "我的世界123abc"
  2. b_text = text.encode() # b'\xe6\x88\x91\xe7\x9a\x84\xe4\xb8\x96\xe7\x95\x8c123abc'

decode:将bytes类型转为字符串

  1. bin_str='111001101000100010010001111001111001101010000100111001001011100010010110111001111001010110001100001100010011001000110011011000010110001001100011'
  2. list_bin = [bin_str[i:i+8] for i in range(0,len(bin_str),8)]
  3. """
  4. ['11100110', '10001000', '10010001', '11100111', '10011010', '10000100', '11100100', '10111000', '10010110', '11100111', '10010101', '10001100', '00110001', '00110010', '00110011', '01100001', '01100010', '01100011']
  5. """
  6. list_int = []
  7. for bin_s in list_bin:
  8. list_int.append(int(bin_s,2))
  9. # [230, 136, 145, 231, 154, 132, 228, 184, 150, 231, 149, 140, 49, 50, 51, 97, 98, 99]
  10. bytes(list_int).decode()
  11. # '我的世界123abc'