数字类型转换 [英] Numeric type conversions
问题描述
我是Python的新手,并且在以下情况下不能轻易找到适当的功能
:
我正在阅读在一个来自串口的字节流中(我已经得到了
与pyserial一起正常工作),它包含一个打包的
二进制格式的数字数据。大部分数据内容都是以整数编码为
2个连续字节,即2字节整数。
我猜应该有一个功能,我可以说
类似于:
My2ByteInt = ConvertToInt(ByteStream [12:13])
在字节流中取一个12位字节和
13的2个字节的随机例子。
任何人都可以指出我在右边朝向合适的功能的方向
拜托?
NB我不知道没有进一步检查确切的字节是什么
编码,但我只是从Windows
VB.Net程序中转录一些代码,这些相同的字节可以直接读入
标准的2字节签名short int,所以我可以确信这是一个相当标准的Windows兼容编码。
John丹恩写道:
我是Python的新手,并且在以下情况下无法轻易找到适当的功能:
我正在从串口读取一个字节流(我已经得到了
与pyserial一起正常工作)并且包含了打包的数字数据
二进制格式。大部分数据内容都是以整数编码为
2个连续字节,即2字节整数。
我猜应该有一个功能,我可以说
类似于:
My2ByteInt = ConvertToInt(ByteStream [12:13])
在字节流中取一个12位字节和
13的2个字节的随机例子。
任何人都可以指出我在右边朝着合适的功能方向
好吗?
查看标准库中的模块结构。
Diez
>
尝试struct.pack
2008-06-17,John Dann< ne ** @ prodata.co.ukwrote:
我正在从一个串口读取一个字节流(我已经得到了
与pyserial一起正常工作)并且包含数字数据打包
二进制格式。大部分数据内容以整数编码为
2个连续字节,即2字节整数。
[snipperdesnip]
任何人都可以指向正确的方向指向合适的功能>
好吗?
ctypes模块在这里应该会有所帮助
此致,
Albert
I''m new to Python and can''t readily find the appropriate function for
the following situation:
I''m reading in a byte stream from a serial port (which I''ve got
working OK with pyserial) and which contains numeric data in a packed
binary format. Much of the data content occurs as integers encoded as
2 consecutive bytes, ie a 2-byte integer.
I''m guess that there should be a function available whereby I can say
something like:
My2ByteInt = ConvertToInt(ByteStream[12:13])
to take a random example of the 2 bytes occurring at positions 12 and
13 in the byte stream.
Can anyone point me in the right direction towards a suitable function
please?
NB I don''t know without further checking exactly how the bytes are
encoded, but I''m just transcribing some code across from a Windows
VB.Net program and these same bytes could be read straight into a
standard 2-byte signed short int, so I can be confident that it''s a
fairly standard Windows-compatible encoding.
John Dann wrote:
I''m new to Python and can''t readily find the appropriate function for
the following situation:
I''m reading in a byte stream from a serial port (which I''ve got
working OK with pyserial) and which contains numeric data in a packed
binary format. Much of the data content occurs as integers encoded as
2 consecutive bytes, ie a 2-byte integer.
I''m guess that there should be a function available whereby I can say
something like:
My2ByteInt = ConvertToInt(ByteStream[12:13])
to take a random example of the 2 bytes occurring at positions 12 and
13 in the byte stream.
Can anyone point me in the right direction towards a suitable function
please?see the module struct in the standard-lib.
Diez
try struct.pack
On 2008-06-17, John Dann <ne**@prodata.co.ukwrote:I''m reading in a byte stream from a serial port (which I''ve got
working OK with pyserial) and which contains numeric data in a packed
binary format. Much of the data content occurs as integers encoded as
2 consecutive bytes, ie a 2-byte integer.[snipperdesnip]
Can anyone point me in the right direction towards a suitable function
please?The ctypes module should be helpful here
Sincerely,
Albert
这篇关于数字类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!