Big / Little Endian交换 [英] Big/Little Endian swap

查看:71
本文介绍了Big / Little Endian交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须交换大/小端,并写出在另一台机器上运行的程序。



当我在另一台机器上读取文件时,它通常会正确读取前360个浮点数,然后开始读取未知数字。未知数字总是相同的,所以它没有被破坏。然后在文件的末尾附近,它再次开始正确读取浮动。



有什么想法吗? fwrite是不是写出了所有字节?



这里是(缩写)代码(在MS VC 10上运行 - 4字节浮点数)

Have to swap big/little endian and write out for a program running on another machine.

When I read the file on the other machine, it will usually read the first 360 floats correctly but then starts reading unknown numbers. The unknown numbers are always the same so it''s not corrupted. Then near the end of the file, it starts reading the floats correctly again.

Any ideas? Is fwrite somehow not writing out all the bytes?

Here''s the (abbreviated) code (running on MS VC 10 - 4 byte float)

float swapEnds(float littleE)
{
	float bigE;
	unsigned char *a, *b;

	a = (unsigned char *) &littleE;
	b = (unsigned char *) &bigE;

	b[0] = a[3];
	b[1] = a[2];
	b[2] = a[1];
	b[3] = a[0];

	return bigE;
}

main()
{
        float r, s;
...
	r = 0;
	for (i = 0; i < NDATA; i++) {
		s = swapEnds(r);

		fwrite( &s, 1, 4, grib );

		r += (float)0.001;
	}
...
}

推荐答案

转换代码看起来正确(你可以验证它)将其结果与 ntohl 的结果进行比较[ ^ ]功能)。

您确定原始文件包含所有格式正确的浮动吗?
Conversion code looks correct (you may verify it comparing its results with the ones of the ntohl[^] function).
Are you sure the original file contains all well-formed floats?


如果它读取第一个360罚款,那么一个问题,然后再次工作,它不太可能是错误的endian交换例程。并且360不是一个特殊的数字,即使被视为1440 - 大小(以字节为单位)。



你看过这些数据了吗?第1440字节周围的数据模式是否有明显变化? 坏区有多大?最后的好区域有多大?文件是以二进制文件还是文本打开的?可能是阅读软件不喜欢某些部分的数据吗?



很抱歉不给你答案,但我认为你需要更多的调查才能给出任何真实的答案。
If it reads the first 360 fine, then has a problem, then works again, it''s unlikely to be the endian swap routine that is at fault. And 360 is not a special number, even when looked at as 1440 - the size in bytes.

Have you looked at the data? Is there any obvious change in data patterns around the 1440th byte? How big is the "bad area"? How big is the "good area" at the end? Is the file opened as binary, or text? Could it be that the reading software does not like some part of the data?

Sorry not to give you an answer, but I think you need more investigation before any kind of true answer can be given.


这篇关于Big / Little Endian交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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