使这个代码段高效 [英] make this snippet efficient

查看:68
本文介绍了使这个代码段高效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *目标 - 从二进制文件中读取整数* /

unsigned int Byte2Int(char * buff)

{

unsigned char * byte = reinterpret_cast< unsigned char *> (buff);

return((byte [0]<< 24)|(byte [1]<< 16)|(byte [2]<< 8)| (byte [3]));

}

/ *主要功能的一部分* /


ifstream fp(" in.bin,ios :: binary);

char buff [4];

fp.read(buff,4);

unsigned int loadSize = Byte2Int(buff);


谢谢。

KK

解决方案

KK写道:

/ *目标 - 从二进制文件读取整数* /
unsigned int Byte2Int(char * buff)
{
unsigned char * byte = reinterpret_cast< unsigned char *> (buff);
return((byte [0]<< 24)|(byte [1]<< 16)|(byte [2]<< 8)|(byte [3 ]);
}
/ *主要功能的一部分* /

ifstream fp(in.bin,ios :: binary);
char buff [4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);




什么'' s *关于它的效率不高吗?


V

-

请在回复时删除资金''A'电子邮件

我没有回复最热门的回复,请不要问




Victor Bazarov写道:

KK写道:

/ *目标 - 从二进制文件读取整数* /
unsigned int Byte2Int(char * buff)
{
unsigned char * byte = reinterpret_cast< unsigned char *> (buff);
return((byte [0]<< 24)|(byte [1]<< 16)|(byte [2]<< 8)|(byte [3 ]);
}
/ *主要功能的一部分* /

ifstream fp(in.bin,ios :: binary);
char buff [4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);



什么是*有效*关于它?

V
-



我必须使用reinterpret_cast运算符吗?我该如何避免呢?


KK发布:

/ *目标 - 从二进制文件读取整数file * /
unsigned int Byte2Int(char * buff)
{
unsigned char * byte = reinterpret_cast< unsigned char *> (buff);
return((byte [0]<< 24)|(byte [1]<< 16)|(byte [2]<< 8)|(byte [3 ]);
}
/ *主要功能的一部分* /

ifstream fp(in.bin,ios :: binary);
char buff [4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);

谢谢。
KK


你没有指定一个字节的位数,但是,看看你的

代码,我们可以做出有根据的猜测。


你没有指定int中的字节数,但是,看看你的

代码,我们可以做出有根据的猜测。


你没有指定存储在文件中的整数的字节顺序,所以我们

只能希望它与系统一样'是的。


你没有指定用于表示文件中数字

的负数系统,所以我们只能希望它'和系统一样。


你不要指定文件中的整数是否包含填充位,或

它们所在的位置,也不指定系统是否存储带填充的
整数比特,或者它们的位置。


使用给出的碎片,试试这个:


unsigned Func(char(&) ;数组)[4])

{

返回reinterpret_cast< int&>(数组);

}

-


Frederick Gotham


/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);

Thank you.
KK

解决方案

KK wrote:

/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);



What''s *INefficient* about it?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



Victor Bazarov wrote:

KK wrote:

/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);



What''s *INefficient* about it?

V
--


Must I use reinterpret_cast operator ? How can I avoid it?


KK posted:

/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cast<unsigned char*> (buff);
return ((byte[0]<<24)|(byte[1]<<16)|(byte[2]<<8)|(byte[3]));
}
/* part of main funciton */

ifstream fp("in.bin",ios::binary);
char buff[4];
fp.read(buff,4);
unsigned int loadSize = Byte2Int(buff);

Thank you.
KK


You don''t specify the amount of bits in a byte, however, looking at your
code, we can make an educated guess of 8.

You don''t specify the amount of bytes in an int, however, looking at your
code, we can make an educated guess of 4.

You don''t specify the byte order of the integer stored in the file, so we
can only hope that it''s the same as the system''s.

You don''t specify the negative number system used to represent the number
in the file, so we can only hope that it''s the same as the system''s.

You don''t specify whether the integer in the file contains padding bits, or
where they''re located, nor do you specify whether the system stores
integers with padding bits, or where they''re located.

Working with the scraps being given, try this:

unsigned Func( char (&array)[4] )
{
return reinterpret_cast<int&>( array );
}
--

Frederick Gotham


这篇关于使这个代码段高效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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