Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net IronPython-将int转换为字节数组_.net_Python_Ironpython_Bytearray - Fatal编程技术网

.net IronPython-将int转换为字节数组

.net IronPython-将int转换为字节数组,.net,python,ironpython,bytearray,.net,Python,Ironpython,Bytearray,在Python中获取字符串长度,然后将该int值转换为字节数组的正确方法是什么?将其打印到控制台进行测试的正确方法是什么?使用.Net: byte[] buffer = System.BitConverter.GetBytes(string.Length) print System.BitConverter.ToString(buffer) 将字节输出为十六进制。您可能需要清理IronPython的语法。请使用 +谢谢。我希望我知道什么是纯Python语法。。。但我一直在接触Python 3文

在Python中获取字符串长度,然后将该int值转换为字节数组的正确方法是什么?将其打印到控制台进行测试的正确方法是什么?

使用.Net:

byte[] buffer = System.BitConverter.GetBytes(string.Length)
print System.BitConverter.ToString(buffer)
将字节输出为十六进制。您可能需要清理IronPython的语法。

请使用


+谢谢。我希望我知道什么是纯Python语法。。。但我一直在接触Python 3文档。抱歉,接下来的问题是,长数据类型是无限的。我需要将值(长度)存储在6个字节中。最好的解释方法是什么。我需要在较小的数字上填充字节。struct模块使用的数据类型与Python中使用的不同。在本例中,“long”是4个字节。6字节对于一个整数来说是一个奇怪的长度,但是你可以用类似struct.pack的东西来完成它('在struct.pack('L',)中对x执行类似于[ord(x)的操作不是更好吗?'如果我用它来转换当前历元,我会得到一个类似于'e0\xf1U'的字符串,使用ord我会得到[100,48,241,85],这看起来更有用。
import struct

print struct.pack('L', len("some string")) # int to a (long) byte array