473,698 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

make this snippet efficient

KK
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cas t<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

Jun 29 '06 #1
14 1845
KK wrote:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cas t<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
Jun 29 '06 #2

Victor Bazarov wrote:
KK wrote:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cas t<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_cas t operator ? How can I avoid it?

Jun 29 '06 #3
KK posted:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cas t<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_cas t<int&>( array );
}
--

Frederick Gotham
Jun 29 '06 #4
> > > unsigned char* byte = reinterpret_cas t<unsigned char*> (buff);
Must I use reinterpret_cas t operator ? How can I avoid it?


use : unsigned char* byte = (unsigned char*)buff;

Jun 29 '06 #5
Frederick Gotham posted:

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

Should have casted to unsigned&, rather than int&.
--

Frederick Gotham
Jun 29 '06 #6
chandu wrote:
unsigned char* byte = reinterpret_cas t<unsigned char*> (buff);

Must I use reinterpret_cas t operator ? How can I avoid it?


use : unsigned char* byte = (unsigned char*)buff;


Which means exactly the same thing without the keyword. How is it
any better?
Jun 29 '06 #7

Frederick Gotham wrote:
KK posted:
/* Target - read an integer from a binary file */
unsigned int Byte2Int(char *buff)
{
unsigned char* byte = reinterpret_cas t<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.

When programming in C++, could one realistically expect to encounter a
system that does not have 8 bits in a byte?

Markus.

Jun 30 '06 #8
Markus Svilans posted:

When programming in C++, could one realistically expect to encounter a
system that does not have 8 bits in a byte?

You're on a Standard C++ newsgroup, and people here like to be pedantic. It
pays off in the long run, you end up with code that will run perfectly for
eons.

Here's a few things that the Standard allows:

(1) Machines need not use two's complement.
(2) Null pointers need not be all-bits-zero.
(3) Bytes need not be eight bits.
(4) Primitive types may contain padding bits.

Either you take all these things into account, and write FULLY-portable and
Standard-compliant code, or you don't.

If it ever got to a point where an old-fashioned constraint was hindering
efficiency or functionality, the constraint would be lifted. But until
then, you use the following macros to tell you how many bits you have in a
byte:
#define CHAR_BIT \
(((unsigned char)-1)/(((unsigned char)-1)%0x3fffffffL+ 1) \
/0x3fffffffL%0x3 fffffffL*30+((u nsigned char)-1)%0x3fffffffL \
/(((unsigned char)-1)%31+1)/31%31*5 + 4-12/(((unsigned char)\
-1)%31+3))

--

Frederick Gotham
Jun 30 '06 #9
Frederick Gotham wrote:
use the following macros to tell you how many bits you have in a
byte:
#define CHAR_BIT \
(((unsigned char)-1)/(((unsigned char)-1)%0x3fffffffL+ 1) \
/0x3fffffffL%0x3 fffffffL*30+((u nsigned char)-1)%0x3fffffffL \
/(((unsigned char)-1)%31+1)/31%31*5 + 4-12/(((unsigned char)\
-1)%31+3))


Why provide an implementation (especially one so... urk), rather than
just explain that this macro is available in the standard header
<climits>?

Luke

Jun 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1871
by: Blankdraw | last post by:
The following 2 code segs seem to be the cause of my output infinite loop. Can someone tell me if this is wrong? I'd like to post the entire program, but that seems to distract everyone who migh post from the actual functional glitch. for (row = 0; row < 52; row++) /* dump array into text file */ {
1
2621
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to Activator.CreateInstance() it always fails. However if I walk my assembly and get a type value, the call to...
4
1464
by: hufel | last post by:
Hi, I'm doing my first big project in C# and I'm stuck with a problem that I believe has a simple and efficient solution for it (I just haven't bumped into it yet...). The concept is the following: //Users manage clients. When the system creates the client it must fetch the information from the DB and allow some modifications: Client client = new Client("test"); MasterAccount ma = new MasterAccount(1324651);
3
1245
by: Jarod | last post by:
Hey I would like customize a little existing snippet in VS. Let's say there is snippet prop that works as a property. And it works pretty nice, but I would like it to automatically insert property name based on private field. So if the private field is named _id the property should be ID. I believe it's not much work but it will make my development a little faster. Will it be hard to change this behaviour ? Jarod
10
2177
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All I am doing is quering the db for all the headers for a certain group and then going through them to find all the parts of each post. I only want ones that are complete. Meaning all segments for that one file posted are there. using System;
19
2433
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; //every class may have this statement self.hello = function() {
12
1719
by: pedagani | last post by:
Dear comp.lang.c++, Could you make this snippet more efficient? As you see I have too many variables introduced in the code. //Read set of integers from a file on line by line basis in a STL set //fp is pre-defined for(;!fp.eof();) { string linestr;
3
2838
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
82
3700
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from. Let's name the update method as doUpdate and stl::map read methods as getData and copyData. Since stl::map is not thread-safe, we should do synchronization by ourselves. A usable solution is to create a boost::mutex::scoped_lock object in all above...
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.