二进制文件 - 数字 [英] Binary file - numbers

查看:108
本文介绍了二进制文件 - 数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件,我想读一些数字。然而

这些数字是32位浮点数。如何将数字输入C

程序才能使用?我可以手动进行计算....但是有更简单的方法吗?有没有一些方便的功能呢?


例子。


文件包含:42 7C 00 00

我想读它并最终得到我的C变量等于63.

I have a binary file that I want to read some numbers out of. However
the numbers are 32bit floats. How can I get the numbers into a C
program to use? I can do the calculations manually....but is there an
easier way? Is there some handy functions which do this?

example.

File contains: 42 7C 00 00
I want to read it in and end up with my C variable equalling 63.

推荐答案

E-Star< un ******* @ linuxmail.org>写道:
E-Star <un*******@linuxmail.org> wrote:
我有一个二进制文件,我想读取一些数字。但是这些数字是32位浮点数。如何将数字输入到一个C
程序中使用?
I have a binary file that I want to read some numbers out of. However
the numbers are 32bit floats. How can I get the numbers into a C
program to use?




嗯......你_could_尝试fread(),但是有一个问题。 fread()将
字节读入内存,而不是值。如果这些浮标不是由一个

系统编写的,它可以完全按照你的方式安排它的浮点数,那么你不会得到你想要的值。

当然,你可以通过一个程序fwrite()

一些花车,然后在同一台机器上有另一个程序fread()

他们以后,但如果你想从另一台机器上读取文件,

你冒了风险,风险越大越不像你自己的

机器文件的来源是。

这是一个平衡需求的问题,真的。如果你知道这个

文件只会包含你的文件有种花车,你选择

简单并使用fread()。如果你知道这个文件是一个交叉的

平台标准,并且你希望你的程序在几种

机器上运行,你可以手工解析它。


Richard



Well... you _could_ try fread(), but there''s a catch. fread() reads
bytes into memory, not values. If those floats weren''t written by a
system that arranges its floats exactly the same way as yours does, you
won''t get the values you expected.
You can, of course, usually get away with having one program fwrite()
some floats, and then having another program on the same machine fread()
them later on, but if you want to read files from another machine,
you''re taking a risk, and the risk is greater the less like your own
machine the source of the file is.
It''s all a question of balancing demands, really. If you know that this
file is only ever going to contain "your" kind of floats, you opt for
simplicity and use fread(). If you know that this file is a cross-
platform standard and you want your program to run on several kinds of
machines, you parse it by hand.

Richard


James Connell写道:
James Connell wrote:
我还在想弄清楚如何*任何*组合的0x42,0x7C,0x0和
0x0 == 63 ?????
I''m still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
0x0 == 63?????




它是63.0的浮动表示


-

pete



It''s a float representation for 63.0

--
pete


pete< pf ***** @ mindspring.com> ;写道:
pete <pf*****@mindspring.com> wrote:
James Connell写道:
James Connell wrote:
我还在试图弄清楚如何*任何*组合0x42,0x7C,0x0并且
0x0 == 63 ?????
I''m still trying to figure out how *any* combo of 0x42, 0x7C, 0x0 and
0x0 == 63?????



它是63.0的浮动表示



It''s a float representation for 63.0




或者更确切地说,它是一个IEEE 60559单精度表示

为63.0。下面的代码产生输出63,并且基于这里的

描述:

http://www.psc.edu/general/software/...ieee/ieee.html


代码未经优化或经过充分测试。


对于实际使用IEEE单浮点数的系统,请记住

他们可能有不同的字节顺序。


Phil T


#include< stdio.h>

#include< math.h>

#include< float.h>

#include< assert.h>

double ieee_single(const void * v)

{

const unsigned char * data = v;

int s,e ;

unsigned long src;

long f;

double value;


src =( (无符号长)数据[0]<<< 24)+

((无符号长)数据[1]<< 16)+

((无符号)长)数据[2]<& LT; 8)+

((无符号长)数据[3]);


s =(src& 0x80000000UL)>> 31;

e =(src& 0x7F800000UL)>> 23;

f =(src& 0x007FFFFFUL);


if(e == 255&& f!= 0){

/ * NaN - 不是数字* /

值= DBL_MAX;

}

else if(e == 255& ;& f == 0&& s == 1){

/ *负无穷大* /

值= -DBL_MAX;

}

else if(e == 255&& f == 0&& s == 0){

/ *正无穷大* /

value = DBL_MAX;

}

else if(e> 0&& e< 255){

/ *正常数字* /

f + = 0x00800000UL;

if(s)f = -f;

value = ldexp (f,e - 127 - 23);

}

if if(e == 0&& f!= 0){

/ *非正常数字* /

if(s)f = -f;

value = ldexp(f,-126 - 23);

}

否则(e == 0&& f == 0&& s == 1){

/ *负零* /

value = 0;

}

else if(e == 0&& f == 0&am磷;&安培; s == 0){

/ *正零* /

值= 0;

}

else {

/ *永远不会发生* /

printf(" s =%d,e =%d,f =%lu \ n",s,e, f);

断言(!哎呀,ieee_single()中未处理的情况);

}


返回价值;

}


int main(无效)

{

float f;

unsigned char combo [] = {0x42,0x7c,0x00,0x00};

printf("%g\ n",ieee_single(combo));

返回0;

}



Or more precisely, it''s an IEEE 60559 single precision representation
for 63.0. The code below produces the output "63", and is based on the
description here:

http://www.psc.edu/general/software/...ieee/ieee.html

The code is not optimised or well tested.

For systems that actually use IEEE single floats, bear in mind that
they might have a different byte order.

Phil T

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <assert.h>

double ieee_single(const void *v)
{
const unsigned char *data = v;
int s, e;
unsigned long src;
long f;
double value;

src = ((unsigned long)data[0] << 24) +
((unsigned long)data[1] << 16) +
((unsigned long)data[2] << 8) +
((unsigned long)data[3]);

s = (src & 0x80000000UL) >> 31;
e = (src & 0x7F800000UL) >> 23;
f = (src & 0x007FFFFFUL);

if (e == 255 && f != 0) {
/* NaN - Not a number */
value = DBL_MAX;
}
else if (e == 255 && f == 0 && s == 1) {
/* Negative infinity */
value = -DBL_MAX;
}
else if (e == 255 && f == 0 && s == 0) {
/* Positive infinity */
value = DBL_MAX;
}
else if (e > 0 && e < 255) {
/* Normal number */
f += 0x00800000UL;
if (s) f = -f;
value = ldexp(f, e - 127 - 23);
}
else if (e == 0 && f != 0) {
/* Denormal number */
if (s) f = -f;
value = ldexp(f, -126 - 23);
}
else if (e == 0 && f == 0 && s == 1) {
/* Negative zero */
value = 0;
}
else if (e == 0 && f == 0 && s == 0) {
/* Positive zero */
value = 0;
}
else {
/* Never happens */
printf("s = %d, e = %d, f = %lu\n", s, e, f);
assert(!"Oops, unhandled case in ieee_single()");
}

return value;
}

int main(void)
{
float f;
unsigned char combo[] = {0x42, 0x7c, 0x00, 0x00};
printf("%g\n", ieee_single(combo));
return 0;
}


这篇关于二进制文件 - 数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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