Python 中如何实现 Java 的 getBytes() 方法
在 Python 中,可以使用字符串的 encode() 方法来实现类似 Java 中 getBytes() 方法的功能。具体实现如下:
# Python 3.x
s = 'Hello World'
b = s.encode('utf-8')
print(b) # b'Hello World'
这里将字符串 s 转换为字节串 b,使用 utf-8 编码。如果需要指定其他编码方式,可以将 utf-8 替换为相应的编码方式即可。
原文地址: https://www.cveoy.top/t/topic/oXmv 著作权归作者所有。请勿转载和采集!