将24位二进制转换为int [英] Conversion of 24bit binary to int

查看:282
本文介绍了将24位二进制转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中是否有一种有效/快速的方法将二进制数据从文件转换为
(24位hex(int)big endian)到32bit int(little endian)?已经看过

struct.unpack,但我不确定Python提供的方式和内容。 Idar


原始数据格式存储在512字的块中

(1536B = 3字节/字)在表格Ch1:1536B(3B * 512)上,二进制(十六进制)数据

是big endian

Ch2:1536B(3B * 512)

Ch3:1536B(3B * 512)

等等


等效的c ++程序如下所示:

for(i = 0; i< nchn; i ++ )

{

for(k = 0; k
{

ar24 [k ] = 0; //输出数组= 32位int array-> Mt24 fmt

pdt =(unsigned char *)(& ar24 [k]);

* pdt = *(a + 2);

*(pdt + 1)= *(a + 1);

*(pdt + 2)= *(a +0);

a + = 3;

ar24 [k] - = DownloadDataOffset;

// printf("%d \ n",ar24 [k]); //这是32位格式的数字

}

}

Is there an effecient/fast way in python to convert binary data from file
(24bit hex(int) big endian) to 32bit int (little endian)? Have seen
struct.unpack, but I am unsure how and what Python has to offer. Idar

The orginal data format is stored in blocks of 512 words
(1536B=3Bytes/word) on the form Ch1: 1536B (3B*512), the binary (hex) data
is big endian
Ch2: 1536B (3B*512)
Ch3: 1536B (3B*512)
and so on

The equivalent c++ program looks like this:
for(i=0;i<nchn;i++)
{
for(k=0;k<segl;k++)
{
ar24[k]=0;//output array=32 bit int array->Mt24 fmt
pdt=(unsigned char *)(&ar24[k]);
*pdt =*(a+2);
*(pdt+1)=*(a+1);
*(pdt+2)=*(a+0);
a+=3;
ar24[k]-=DownloadDataOffset;
// printf("%d\n",ar24[k]);//this is the number on 32 bit format
}
}

推荐答案

Idar写道:

在python中是否有一种有效/快速的方式来转换文件中的二进制数据
(24b它hex(int)big endian)到32bit int(little endian)?已经看过
struct.unpack,但我不确定Python的提供方式和内容。伊达尔

Is there an effecient/fast way in python to convert binary data from file
(24bit hex(int) big endian) to 32bit int (little endian)? Have seen
struct.unpack, but I am unsure how and what Python has to offer. Idar




我认为问题不明确。你说你见过struct.unpack。

那么呢?你不觉得struct.unpack会起作用吗?你是什​​么?b $ b意味着你不确定Python提供的方式和内容是什么?网站上的文档

清楚地解释了
提供的struct.unpack
的方式和内容......


请澄清。


-Peter



I think the question is unclear. You say you''ve seen struct.unpack.
So what then? Don''t you think struct.unpack will work? What do you
mean you are unsure how and what Python has to offer? The documentation
which is on the web site clearly explains how and what struct.unpack
has to offer...

Please clarify.

-Peter


如果我理解正确,十六进制与此无关&

数据实际上是二进制的,所以你要找的可能是:
If I''m understanding correctly, hex has nothing to do with this and the
data is really binary, so what you''re looking for is probably:
data =''\\\\\002''
temp = struct.unpack(''>我',''\'''+数据)#pad to 4-byte unsigned
big-endian整数格式print temp#现在是一个普通的python整数(在一个元组中)
(258L,)print repr(struct.pack(''< I'',* temp) )#code in 4-byte unsigned
data = ''\000\001\002''
temp = struct.unpack( ''>I'', ''\000''+data ) # pad to 4-byte unsigned big-endian integer format print temp # is now a regular python integer (in a tuple) (258L,) print repr(struct.pack( ''<I'', *temp )) # encode in 4-byte unsigned


little-endian整数格式

''\ x02 \ x01 \ x00 \ x00''


如果你有很多这样的数据,有更快的方法(例如,PIL会

可能有东西可以操纵RGB到RGBA图像),类似地,你可以使用Numpy同时添加大量行(如果

我理解你对数据的正确描述)。在不知道

正在加载什么类型的数据的情况下,很难给出更好的推荐。


HTH,

Mike

Idar写道:

在python中是否有一种有效/快速的方法将二进制数据从
文件(24位十六进制(int)大端)转换为32位int(小端)?已经看过struct.unpack,但我不确定Python提供的方式和内容。
Idar

little-endian integer format
''\x02\x01\x00\x00''

There are faster ways if you have a lot of such data (e.g. PIL would
likely have something to manipulate RGB to RGBA images), similarly, you
could use Numpy to add large numbers of rows simultaneously (all 512 if
I understand your description of the data correctly). Without knowing
what type of data is being loaded it''s hard to give a better recommendation.

HTH,
Mike
Idar wrote:
Is there an effecient/fast way in python to convert binary data from
file (24bit hex(int) big endian) to 32bit int (little endian)? Have
seen struct.unpack, but I am unsure how and what Python has to offer.
Idar




....

_______________________________________

Mike C. Fletcher

设计师,VR水管工,编码器
http://members.rogers.com/mcfletch/




....
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Idar写道:
Idar wrote:
在python中是否有一种有效/快速的方法将二进制数据从文件
(24位十六进制(int)大端)转换为32位int (小端)?已经看过
struct.unpack,但我不确定Python的提供方式和内容。 Idar
Is there an effecient/fast way in python to convert binary data from file
(24bit hex(int) big endian) to 32bit int (little endian)? Have seen
struct.unpack, but I am unsure how and what Python has to offer. Idar




正如彼得提到的,你还没有给出足够的信息

关于你需要什么,但这里有一些代码会做什么

我_think_你说你想要...


这段代码假设你有一个字符串(这里名为teststr)
$ b您描述的源格式的$ b。您可以通过多种方式获得这样的字符串

,例如:通过从文件对象中读取。


此代码然后交换每3个字符,并在每组三个字符之间插入一个空的

字节。


结果在一个列表中,可以很容易地通过''''。join()将
转换回字符串,如测试打印输出所示。 />

我希望数组模块或Numpy能用_exactly_同样的技术更快地工作,但是我是

现在没有足够的时间来检查它。


如果在使用数组或NumPy之后这还不够快(或者在Alex,Tim之后使用

,通过它,我会

强烈推荐Pyrex - 你可以用你的C ++代码完全相同的

强制执行。

teststr =''''。join([chr(i)for i in range(128,128 + 20 * 3)]


result = len( teststr)* 4 // 3 * [chr(0)]

for x in range(3):

result [2-x :: 4] = testst r [x :: 3]


print repr(''''。join(result))

问候,

Pat



As Peter mentions, you haven''t _really_ given enough information
about what you need, but here is some code which will do what
I _think_ you said you want...

This code assumes that you have a string (named teststr here)
in the source format you describe. You can get a string
like this in several ways, e.g. by reading from a file object.

This code then swaps every 3 characters and inserts a null
byte between every group of three characters.

The result is in a list, which can easily be converted back
to a string by ''''.join() as shown in the test printout.

I would expect that either the array module or Numpy would
work faster with _exactly_ the same technique, but I''m
not bored enough to check that out right now.

If this isn''t fast enough after using array or NumPy (or
after Alex, Tim, et al. get through with it), I would
highly recommend Pyrex -- you can do exactly the same
sorts of coercions you were doing in your C++ code.
teststr = ''''.join([chr(i) for i in range(128,128+20*3)])

result = len(teststr) * 4 // 3 * [chr(0)]
for x in range(3):
result[2-x::4] = teststr[x::3]

print repr(''''.join(result))
Regards,
Pat


这篇关于将24位二进制转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆