【发布时间】:2010-10-22 05:23:33
【问题描述】:
在 Python 中获取字符串长度,然后将该 int 转换为字节数组的正确方法是什么?将其打印到控制台进行测试的正确方法是什么?
【问题讨论】:
标签: .net python ironpython bytearray
在 Python 中获取字符串长度,然后将该 int 转换为字节数组的正确方法是什么?将其打印到控制台进行测试的正确方法是什么?
【问题讨论】:
标签: .net python ironpython bytearray
使用struct。
import struct
print struct.pack('L', len("some string")) # int to a (long) byte array
【讨论】:
使用.Net:
byte[] buffer = System.BitConverter.GetBytes(string.Length)
print System.BitConverter.ToString(buffer)
这会将字节输出为十六进制。您可能需要清理 IronPython 的语法。
【讨论】: