San*_*nda 12 python java bytearray
在我的Java代码中,我有以下代码段:
String secret = "secret";
byte[] thebytes = secret.getBytes();
Run Code Online (Sandbox Code Playgroud)
我想在python中得到完全相同的结果.我怎样才能做到这一点 ?
secret = 'secret'
thebytes = ??? ??? ???
Run Code Online (Sandbox Code Playgroud)
谢谢.
编辑:
另外,拥有Python 2.x和3.x的解决方案会很有趣
这并不像最初看起来那么简单,因为Python历史上混淆了字节数组和字符串.简而言之,在Python 3中,是
secret = "secret"
secret.encode()
Run Code Online (Sandbox Code Playgroud)
但是你应该阅读Python如何处理unicode,字符串和字节.
在python-2.7中有bytearray():
>>> s = 'secret'
>>> b = bytearray(s)
>>> for i in b:
... print i
115
101
99
114
101
116
Run Code Online (Sandbox Code Playgroud)
如果这是你正在寻找的.
| 归档时间: |
|
| 查看次数: |
13427 次 |
| 最近记录: |