将char转换为float(从文件中读取二进制数据) [英] converting char to float (reading binary data from file)

查看:168
本文介绍了将char转换为float(从文件中读取二进制数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将我从二进制文件中读取的一些char数据(使用

ifstream)转换为float类型。我已经成功转换了int类型,但现在我需要做浮点类型但它似乎没有工作。

下面的代码是我正在尝试使用的。有人看到任何明显的

错误吗?或者有任何提示/指示?

问候,

Igor


float floatRead = 0;

UINT32 * ptr =(UINT32 *)(& floatRead);


int offset = 0;


for( int ii = startInd; ii< = endInd; ii ++){

* ptr | =(charBuf [ii]<< offset);

offset + = 8;

};

Hi,
I''m trying to convert some char data I read from a binary file (using
ifstream) to a float type. I''ve managed to convert the int types but
now I need to do the float types as well but it doesn''t seem to work.
The code below is what I''m trying to use. Anyone see any obvious
errors? or have any hints/pointers?
regards,
Igor

float floatRead = 0;

UINT32* ptr = (UINT32 *) (&floatRead);

int offset = 0;

for (int ii=startInd; ii<=endInd; ii++){
*ptr |= (charBuf[ii] << offset);
offset += 8;
};

推荐答案

5月21日上午11:29 * am,Michael DOUBEZ< michael.dou .. 。@ free.frwrote:
On May 21, 11:29*am, Michael DOUBEZ <michael.dou...@free.frwrote:

itdevriesaécrit:
itdevries a écrit :

我正试图转换一些char数据我从二进制文件(使用

ifstream)读取到float类型。我已经成功转换了int类型,但现在我需要做浮点类型但它似乎没有工作。

下面的代码是我正在尝试使用的。有人看到任何明显的

错误吗?或者有任何提示/指示?
I''m trying to convert some char data I read from a binary file (using
ifstream) to a float type. I''ve managed to convert the int types but
now I need to do the float types as well but it doesn''t seem to work.
The code below is what I''m trying to use. Anyone see any obvious
errors? or have any hints/pointers?



我认为charBuf的类型为char []?在这种情况下,'charBuf [ii]<<

offset''在偏移> = 8时为0,因此您只得到0。


I suppose charBuf is of the type char[] ? In this case ''charBuf[ii] <<
offset'' is 0 as soon as offset>=8 so you get only 0s.


float floatRead = 0;
float floatRead = 0;


UINT32 * * * ptr * =(UINT32 *)(& floatRead);
UINT32* * *ptr *= (UINT32 *) (&floatRead);


int offset * = 0;
int offset * = 0;


for(int ii = startInd; ii< = endInd; ii ++){

* * ptr | =(charBuf [ii]<< offset);

*抵消* + = 8;

};
for (int ii=startInd; ii<=endInd; ii++){
* *ptr |= (charBuf[ii] << offset);
* offset *+= 8;
};



你对bitwise运算符的使用看起来很笨拙,除非你有一些逻辑要求
处理不同的字节顺序。




memcpy(& floatRead,charBuf + startInd,sizeof(floatRea d))有什么问题;




-

迈克尔


Your use of bitwise operator looks clumsy unless you have some logic to
handle different byte ordering.

What''s wrong with
memcpy(&floatRead,charBuf+startInd,sizeof(floatRea d));
?

--
Michael



谢谢,这有效......但是,我不明白是什么'我的

原始代码错了。任何想法?

Igor

thanks, that works... however, I don''t understand what''s wrong with my
original code. any ideas?
Igor


5月21日,12:23 * pm,Michael DOUBEZ< michael.dou .. 。@ free.frwrote:
On May 21, 12:23*pm, Michael DOUBEZ <michael.dou...@free.frwrote:

itdevriesaécrit:
itdevries a écrit :

5月21日上午11:29 ,Michael DOUBEZ< michael.dou ... @ free.frwrote:
On May 21, 11:29 am, Michael DOUBEZ <michael.dou...@free.frwrote:

itdevriesaécrit:
float floatRead = 0;
UINT32 * * * ptr * =(UINT32 *)(& floatRead);
int offset * = 0;
for(int ii = startInd; ii< = endInd; ii ++){
* * ptr | =(charBuf [ii]<< offset);
* offset * + = 8;
};
itdevries a écrit :
float floatRead = 0;
UINT32* * *ptr *= (UINT32 *) (&floatRead);
int offset * = 0;
for (int ii=startInd; ii<=endInd; ii++){
* *ptr |= (charBuf[ii] << offset);
* offset *+= 8;
};


我不明白我的

原始代码出了什么问题。有任何想法吗?
I don''t understand what''s wrong with my
original code. any ideas?



展开你的循环:

* ptr | =(charBuf [startInd + 0]<< * 0);

* ptr | =(charBuf [startInd + 1]<< * 8);

* ptr | =(charBuf [startInd + 2]<< 16);

* ptr | =(charBuf [startInd + 3]<< 24);


成为(正如我所提到的):

* ptr | = charBuf [startInd];

* ptr | = 0;

* ptr | = 0;

* ptr | = 0;


因为你将一个字符串的位移比它的大小更多(通常是

8)所以它变为0。 />

示例:

char x = 0xBF;

断言((x << 8)== 0);

断言((x << 12)== 0);

断言((x << 42)== 0);


-

Michael


Unrolling your loop:
*ptr |= (charBuf[startInd+0] << *0);
*ptr |= (charBuf[startInd+1] << *8);
*ptr |= (charBuf[startInd+2] << 16);
*ptr |= (charBuf[startInd+3] << 24);

Becomes (as I have mentionned):
*ptr |= charBuf[startInd];
*ptr |= 0;
*ptr |= 0;
*ptr |= 0;

Because you shift-left a char more times than its size in bits (usually
8) so it becomes 0.

Example:
char x=0xBF;
assert( (x<< 8) == 0);
assert( (x<<12) == 0);
assert( (x<<42) == 0);

--
Michael



非常感谢您花时间回复。我理解你正在使用的逻辑

,这是我对

代码的最初担忧之一,但它似乎适用于int类型(也许通过

巧合)所以我认为它也适用于浮点类型。然而,我不明白的一件事是,当我通过调试器逐步完成循环

时,即使从

第2步我以为我会做'* ptr | = 0"我认为不应该改变浮动的价值。这让我得出的结论是,

按位变换与我的预期不同。

Igor

Thanks so much for taking the time to respond. I understand the logic
you''re using and it was one of the initial concerns I had with the
code, however it seemed to work fine for int types (maybe by
coincidence) so I thought it would work for float types as well. One
thing I don''t understand however is that when I step through the loop
with the debugger I see the value of the float change even though from
step 2 I thought I''d be doing "*ptr |= 0" which I thought shouldn''t
alter the value of the float. that lead me to the conclusion that the
bitwise shift worked differently from what I expected.
Igor


On 21 mai,11:29,Michael DOUBEZ< michael.dou ... @ free.frwrote:
On 21 mai, 11:29, Michael DOUBEZ <michael.dou...@free.frwrote:

itdevriesaécrit:
itdevries a écrit :

我正在尝试将从二进制文件(使用

ifstream)读取的一些char数据转换为float类型。我已经成功转换了int类型,但现在我需要做浮点类型但它似乎没有工作。

下面的代码是我正在尝试使用的。有人看到任何明显的

错误吗?或者有任何提示/指示?
I''m trying to convert some char data I read from a binary file (using
ifstream) to a float type. I''ve managed to convert the int types but
now I need to do the float types as well but it doesn''t seem to work.
The code below is what I''m trying to use. Anyone see any obvious
errors? or have any hints/pointers?



我认为charBuf的类型为char []?在这种情况下,'charBuf [ii]<<

offset''在偏移> = 8时为0,因此您只得到0。


I suppose charBuf is of the type char[] ? In this case ''charBuf[ii] <<
offset'' is 0 as soon as offset>=8 so you get only 0s.


float floatRead = 0;
float floatRead = 0;


UINT32 * ptr =(UINT32 *)(& floatRead);
UINT32* ptr = (UINT32 *) (&floatRead);


int offset = 0;
int offset = 0;


for(int ii = startInd; ii< = endInd; ii ++){

* ptr | =(charBuf [ ii]<< offset);

抵消+ = 8;

};
for (int ii=startInd; ii<=endInd; ii++){
*ptr |= (charBuf[ii] << offset);
offset += 8;
};



你对bitwise运算符的使用看起来很笨拙,除非你有一些逻辑要求
处理不同的字节顺序。




memcpy(& floatRead,charBuf + startInd,sizeof(floatRea d))有什么问题;




-

Michael


Your use of bitwise operator looks clumsy unless you have some logic to
handle different byte ordering.

What''s wrong with
memcpy(&floatRead,charBuf+startInd,sizeof(floatRea d));
?

--
Michael


这篇关于将char转换为float(从文件中读取二进制数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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