雪辉 发表于 2021-5-20 16:23

Python怎么把字符串转字节集

本帖最后由 雪辉 于 2021-5-20 16:26 编辑

C#
byte[] temp = Encoding.UTF8.GetBytes(“我是中文”);
输出230,136,145,230,152,175,228,184,173,230,150,135

Python
sj = "我是中文"
sj = sj.encode('utf8')
输出 b'\xe6\x88\x91\xe6\x98\xaf\xe4\xb8\xad\xe6\x96\x87'




python怎么才能做到其他编程语言的转字节集呢

雪辉 发表于 2021-5-20 16:24

python怎么才能做到其他编程语言的转字节集呢?

qeq66 发表于 2021-5-20 16:29

https://gist.github.com/igniteflow/1237391
看看这个?

雪辉 发表于 2021-5-20 16:49

qeq66 发表于 2021-5-20 16:29
看看这个?

我进去怎么提示404

qeq66 发表于 2021-5-20 16:55

雪辉 发表于 2021-5-20 16:49
我进去怎么提示404

import base64

"""
Some useful functions for interacting with Java web services from Python.
"""

def make_file_java_byte_array_compatible(file_obj):
    """
    Reads in a file and converts it to a format accepted as Java byte array
    :param file object
    :return string
    """
    encoded_data = base64.b64encode(file_obj.read())
    strg = ''
    for i in xrange((len(encoded_data)/40)+1):
      strg += encoded_data
      
    return strg

def java_byte_array_to_binary(file_obj):
    """
    Converts a java byte array to a binary stream
    :param java byte array as string (pass in as a file like object, can use StringIO)
    :return binary string
    """
    decoded_data = base64.b64decode(file_obj.read())
    strg = ''
    for i in xrange((len(decoded_data)/40)+1):
      strg += decoded_data
      
    return strg

咸鱼灭 发表于 2021-5-20 17:14

sj = "我是中文".encode()
print(list(sj))

cc78947 发表于 2021-5-21 15:47

Python
sj = "我是中文"
sj = sj.encode('utf8')
mum_lit = [ int(i) for i in sj]
print(mum_lit)
>>>
是这个意思吗,编码就是16进制的
页: [1]
查看完整版本: Python怎么把字符串转字节集