在我的 Java 代码中有以下片段:
String secret = "secret";
byte[] thebytes = secret.getBytes();
我想在 python 中得到完全相同的结果。我该怎么做?
secret = 'secret'
thebytes = ??? ??? ???
谢谢。
编辑:
此外,如果有 Python 2.x 和 3.x 的解决方案会很有趣
最佳答案
这并不像乍看起来那么简单,因为 Python 历来将字节数组和字符串混为一谈。在 Python 3 中,简短的回答是
secret = "secret"
secret.encode()
但是您应该阅读 Python 如何处理 unicode、字符串和字节。
关于java - Java String.getBytes() 和 Python string -> bytes 之间的行为完全相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9375661/