通过重新排序将二进制char数组转换为整数 [英] Convert binary char array to integer with reordering

查看:68
本文介绍了通过重新排序将二进制char数组转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个4字节的char数组,其中包含两个16位带符号

整数的二进制数据,如下所示:


索引3 2 1 0

数据Bh Bl Ah Al


其中Bh是签名16的高字节-bit整数B等等。


我想用char

数组中的数据创建32位整数A和B.


我尝试了类似的东西(和其他各种排列):


A =(数据[1]<< 8)| (unsigned int)data [0];

B =(data [3]<< 8)| (unsigned int)数据[2];


,除了说数据[1] = 0x00和数据[0] = 0x80时,它有效。

在这种情况下,Al得到的标志一直延伸到A的顶部给予

0xffffff80当然是错误的。


我读到关于算术收敛而且我相信正是这些

正在将正确的操作数转换为签名并导致标志

扩展。


那一刻,我这样做是对的:


int A;

int B;

char buildA [4], buildB [4],数据[4];


// data []在这里填充


buildA [0] = data [0 ];

buildA [1] =数据[1];


if(data [1]>> 7)

{

buildA [2] =(char)0xff;

buildA [3] =(char)0xff;

}

else

{

buildA [2] = 0x00;

buildA [3] = 0x00;

}


buildB [0] =数据[2];

buildB [1] = data [3];


if(data [3]>> 7)

{

buildB [2] =(char)0xff;

buildB [3] =(char)0xff;

}

其他

{

buildB [2] = 0x00;

buildB [3 ] = 0x00;

}


A = *((int *)buildA);

B = *((int *)buildB);


当然有一种更清洁的方式?


非常感谢你的时间,


Dave

Hi all,

I have a 4 byte char array with the binary data for two 16-bit signed
integers in it like this:

Index 3 2 1 0
Data Bh Bl Ah Al

Where Bh is the high byte of signed 16-bit integer B and so on.

I want to create 32-bit integers A and B with the data in the char
array.

I have tried things like (and various other permutations):

A = (data[1] << 8) | (unsigned int)data[0];
B = (data[3] << 8) | (unsigned int)data[2];

and this works except for when say data[1] = 0x00 and data[0] = 0x80.
In this case, Al gets sign extended all the way to the top of A giving
0xffffff80 which is wrong of course.

I read about the arithmetic converions and I believe it is these that
are converting the right operand to signed and causing the sign
extension.

At the moment, I am getting things right like this:

int A;
int B;
char buildA[4], buildB[4], data[4];

// data[] gets filled here

buildA[0] = data[0];
buildA[1] = data[1];

if (data[1] >> 7)
{
buildA[2] = (char)0xff;
buildA[3] = (char)0xff;
}
else
{
buildA[2] = 0x00;
buildA[3] = 0x00;
}

buildB[0] = data[2];
buildB[1] = data[3];

if (data[3] >> 7)
{
buildB[2] = (char)0xff;
buildB[3] = (char)0xff;
}
else
{
buildB[2] = 0x00;
buildB[3] = 0x00;
}

A = *((int*)buildA);
B = *((int*)buildB);

Surely there is a cleaner way?

Many thanks for your time,

Dave

推荐答案

文章< 11 *************** ******@g44g2000cwa.googlegroups。 com>,

Dave< do ******** @ yahoo.co.uk>写道:
In article <11*********************@g44g2000cwa.googlegroups. com>,
Dave <do********@yahoo.co.uk> wrote:
大家好,

我有一个4字节的字符数组,其中包含两个16位带符号整数的二进制数据,如下所示:

索引3 2 1 0
数据Bh Bl Ah Al

其中Bh是有符号16位整数B的高字节,依此类推。
Hi all,

I have a 4 byte char array with the binary data for two 16-bit signed
integers in it like this:

Index 3 2 1 0
Data Bh Bl Ah Al

Where Bh is the high byte of signed 16-bit integer B and so on.




不便携。不能在这里讨论。 Blah,等等,等等。



Not portable. Can''t discuss it here. Blah, blah, blah.


如果将signed char转换为unsigned int,结果是

sign-extended ,因为char已签名。尝试:

A =(数据[1]<< 8)| (unsigned char)data [0];

B =(data [3]<< 8)| (unsigned achr)data [2];

(或者使类型为unsigned char的数组)


现在,unsigned char默认提升为signed int ,并且签名的int

应该与unsigned char具有相同的值...


DickB


" Dave和QUOT; <做******** @ yahoo.co.uk>在消息中写道

news:11 ********************* @ g44g2000cwa.googlegro ups.com ...
If you convert a signed char to an unsigned int, the result is
sign-extended, because the char is signed. Try:
A = (data[1] << 8) | (unsigned char)data[0];
B = (data[3] << 8) | (unsigned achr)data[2];
(Or make the array of type unsigned char)

Now, the unsigned char is default promoted to signed int, and the signed int
should have the same value as the unsigned char...

DickB

"Dave" <do********@yahoo.co.uk> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
大家好,

我有一个4字节的char数组,其中包含两个16位有符号整数的二进制数据,如下所示:

索引3 2 1 0
数据Bh Bl Ah Al

其中Bh是带符号的16位整数B的高字节,依此类推。

我想创建32位整数A和B以及char
数组中的数据。

我尝试了类似的东西(以及其他各种排列):

A =(数据[1]<< 8)| (unsigned int)data [0];
B =(data [3]<< 8)| (unsigned int)数据[2];

并且除了说数据[1] = 0x00和数据[0] = 0x80之外都有效。
在这种情况下,Al得到符号扩展一直到A顶部给出
0xffffff80当然是错误的。

我读到了算术转换,我相信正是这些转换正确的操作数签署并导致签署
扩展。

目前,我正在做这样的事情:

int A;
int B;
char buildA [4],buildB [4],数据[4];

// data []在这里填充

buildA [0] =数据[0];
buildA [1] =数据[1];

if(data [1]>> 7)
{
buildA [2 ] =(char)0xff;
buildA [3] =(char)0xff;
}

{
buildA [2] = 0x00;
buildA [3] = 0x00;
}

buildB [0] = data [2];
buildB [1] = data [3];

if(data [3]>> 7)
{
buildB [2] =(char)0xff;
buildB [3] =(char)0xff;
}

{
buildB [2] = 0x00;
buildB [3] = 0x00;
}

A = *( (int *)buildA);
B = *((int *)buildB);

当然有一种更清洁的方式吗?

非常感谢你的时间,
Hi all,

I have a 4 byte char array with the binary data for two 16-bit signed
integers in it like this:

Index 3 2 1 0
Data Bh Bl Ah Al

Where Bh is the high byte of signed 16-bit integer B and so on.

I want to create 32-bit integers A and B with the data in the char
array.

I have tried things like (and various other permutations):

A = (data[1] << 8) | (unsigned int)data[0];
B = (data[3] << 8) | (unsigned int)data[2];

and this works except for when say data[1] = 0x00 and data[0] = 0x80.
In this case, Al gets sign extended all the way to the top of A giving
0xffffff80 which is wrong of course.

I read about the arithmetic converions and I believe it is these that
are converting the right operand to signed and causing the sign
extension.

At the moment, I am getting things right like this:

int A;
int B;
char buildA[4], buildB[4], data[4];

// data[] gets filled here

buildA[0] = data[0];
buildA[1] = data[1];

if (data[1] >> 7)
{
buildA[2] = (char)0xff;
buildA[3] = (char)0xff;
}
else
{
buildA[2] = 0x00;
buildA[3] = 0x00;
}

buildB[0] = data[2];
buildB[1] = data[3];

if (data[3] >> 7)
{
buildB[2] = (char)0xff;
buildB[3] = (char)0xff;
}
else
{
buildB[2] = 0x00;
buildB[3] = 0x00;
}

A = *((int*)buildA);
B = *((int*)buildB);

Surely there is a cleaner way?

Many thanks for your time,

Dave



2005-10-25,Kenny McCormack< ga ***** @ yin.interaccess .COM>写道:
On 2005-10-25, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
文章< 11 ********************* @ g44g2000cwa.googlegroups。 com>,
Dave< do ******** @ yahoo.co.uk>写道:
In article <11*********************@g44g2000cwa.googlegroups. com>,
Dave <do********@yahoo.co.uk> wrote:
大家好,

我有一个4字节的字符数组,其中包含两个16位带符号整数的二进制数据,如下所示:

索引3 2 1 0
数据Bh Bl Ah Al

其中Bh是有符号16位整数B的高字节,依此类推。
Hi all,

I have a 4 byte char array with the binary data for two 16-bit signed
integers in it like this:

Index 3 2 1 0
Data Bh Bl Ah Al

Where Bh is the high byte of signed 16-bit integer B and so on.



不便携。不能在这里讨论。 Blah,等等,等等。



Not portable. Can''t discuss it here. Blah, blah, blah.




说谁?


int16_t A =数据[1]<< 8+ data [0];

int16_t B =数据[3]<< 8&data [2];


对我来说是可移植的。字符必须至少为8位[代表值

从-127到127签名,0到255无符号,它们必须是],而int16_t其中

存在正好是16位并签名。现在理想情况下你应该使用未签名的

字符,但我不认为这在这种情况下确实很重要[好吧,我

假设负零可能仍然是非twos中的陷阱表示

补充系统]。


现在,关于整数值的那些字节的精确_meaning_你

最终可能会有不同的负数[在这种情况下,高位

的字节1或3设置]可以用三种不同的方式表示

根据标准,但假设他首先以便携式方式获得字节值,并且在他生成
的同一台机器上使用它们他们可以把它们放回去,就像他把它们拿出来一样,并且假设它是b
他使用了一个保证恰好是16位的类型(例如,c99 int16_t,< stdint.h>)

这样做他没有丢失任何信息。


虽然这个练习看似毫无意义,但可能是我作为

序列化的一种方法[在这种情况下,他可能希望保证特定的

签名表示他的值以及字节顺序]


我无法想象(除了可能签署字符的可能性之外)

这个不便携?使用的名称是否类似(但实际上并不像b $ b实际上相同)x86寄存器让你认为不便携!


-

我的第一篇文章怎么样?



says who?

int16_t A = data[1]<<8+data[0];
int16_t B = data[3]<<8+data[2];

looks portable to me. chars have to be at least 8 bits [to represent values
from -127 to 127 signed, 0 to 255 unsigned, they have to be], and int16_t where
present is exactly 16 bits and signed. Now ideally you should be using unsigned
char for this, but I don''t think it actually matters in this case [well, I
suppose negative zero could still be a trap representation on non twos
complement systems].

Now, the precise _meaning_ of those bytes with regards to the integer value you
end up with may differ in that negative numbers [in this case, where high bit
of byte 1 or 3 is set] can be represented in precisely three different ways
according to the standard, but assuming that he got the byte values in a
portable way in the first place and is using them on the same machine where he
generated them, he can put them back the same way he got them out, and assuming
he used a type guaranteed to be exactly 16 bits (say, c99 int16_t, <stdint.h>)
he loses no information in doing so.

While this exercise may seem pointless, it could be intended as a method of
serialization [in which case, though, he may wish to guarantee a particular
signed representation of his values as well as a byte order]

I can''t imagine what (other than the possibility that char may be signed) is
non-portable about this? Was it the use of names that resemble (but aren''t
actually the same as) x86 registers that made you think "non-portable!"?

--
How''s that for my first post?


这篇关于通过重新排序将二进制char数组转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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