Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   converting char to int (reading from a binary file) (http://www.velocityreviews.com/forums/t615222-converting-char-to-int-reading-from-a-binary-file.html)

itdevries 05-16-2008 06:32 PM

converting char to int (reading from a binary file)
 
Hi,
I'm trying to read some binary data from a file, I've read a few bytes
of the data into a
char array with ifstream. Now I know that the first 4 bytes in the
char array represent
an integer. How do I go about converting the elements to an integer?
regards, Igor

sebastian 05-16-2008 06:36 PM

Re: converting char to int (reading from a binary file)
 
basically:

int i = *( ( int * )ptr )

itdevries 05-16-2008 07:07 PM

Re: converting char to int (reading from a binary file)
 
On May 16, 8:43 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> sebastian wrote:
> > basically:

>
> > int i = *( ( int * )ptr )

>
> That is a very bad idea, 'ptr' may not be correctly aligned. It would
> be much better to supply the address of 'i' to the procedure that reads
> the bytes, something like
>
> int i = 0;
> myfile.read(&i, sizeof(int));
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


thanks for your response. I'm not 100% sure I understand what you mean
by correctly aligned, would you mind clarifying? I also can't get your
code snippet to work; I get the following compile error:

"Error 1 error C2664: 'std::basic_istream<_Elem,_Traits>::read' :
cannot convert parameter 1 from 'int' to 'char *' "

kind regards,
Igor

itdevries 05-16-2008 07:08 PM

Re: converting char to int (reading from a binary file)
 
On May 16, 8:36 pm, sebastian <sebastianga...@gmail.com> wrote:
> basically:
>
> int i = *( ( int * )ptr )


thanks for your response, it does the trick...
Igor

itdevries 05-16-2008 09:15 PM

Re: converting char to int (reading from a binary file)
 
On May 16, 9:50 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> itdevries wrote:
> > On May 16, 8:43 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> >> sebastian wrote:
> >>> basically:
> >>> int i = *( ( int * )ptr )
> >> That is a very bad idea, 'ptr' may not be correctly aligned. It would
> >> be much better to supply the address of 'i' to the procedure that reads
> >> the bytes, something like

>
> >> int i = 0;
> >> myfile.read(&i, sizeof(int));

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

>
> > thanks for your response. I'm not 100% sure I understand what you mean
> > by correctly aligned, would you mind clarifying?

>
> On some hardware objects of certain sizes (like 'int') need to exist in
> memory at addresses with certain properties, like divisible by the size
> of the object, for example. In such systems a 'char' can lie on the odd
> byte boundary, which may not necessarily be acceptable for an 'int' that
> need an address divisible by, say, 4. Attempt to access the object (by
> dereferencing the pointer formed by casting a pointer to char) can
> trigger a hardware exception.
>
> > I also can't get your

>
> > code snippet to work; I get the following compile error:

>
> > "Error 1 error C2664: 'std::basic_istream<_Elem,_Traits>::read' :
> > cannot convert parameter 1 from 'int' to 'char *' "

>
> You probably missed the '&'. Also, to convert a pointer to 'int' to a
> pointer to 'char' you may need to use 'reinterpret_cast' (which I didn't
> use).
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Victor,
Many thanks for taking the time to explain!
I think I understand what you're saying, do you know what the chances
are of this happening
on a win32 platform?
regards,
Igor

Jim Langston 05-16-2008 09:16 PM

Re: converting char to int (reading from a binary file)
 
itdevries wrote:
> On May 16, 8:36 pm, sebastian <sebastianga...@gmail.com> wrote:
>> basically:
>>
>> int i = *( ( int * )ptr )

>
> thanks for your response, it does the trick...
> Igor


Be aware that depending on your OS this may break at times, not at others,
or always work. It depends on your OS mainly and if it requires intergers
to by specifcally byte aligned. I know that this will work on Windows
systems fine. I understand that wrong alignment it will break on Sun
systems.

If this is platform specific for you and you will never run it on another
platform and you're sure that your system won't break on byte misalligned
integers it should be fine to use. If you ever plan on running the code on
another system then you'll need to do it another way.


--
Jim Langston
tazmaster@rocketmail.com



James Kanze 05-16-2008 09:31 PM

Re: converting char to int (reading from a binary file)
 
On 16 mai, 20:43, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> sebastian wrote:
> > basically:


> > int i = *( ( int * )ptr )


> That is a very bad idea, 'ptr' may not be correctly aligned.


Not to mention issues of size and representation. (As an
extreme case, I know of one machine which uses 6 byte signed
magnitude ints.)

The original poster didn't begin to give enough information with
regards to the input format for us to say, but if it's a
standard Internet protocol, then you read an int with something
like:

int32_t
getInt( std::istream& source )
{
uint32_t result = source.get() << 24 ;
result |= source.get() << 16 ;
result |= source.get() << 8 ;
result |= source.get() ;
return result ;
}

Except that you'd add some error handling. (And of course, if
you don't have int32_t and uint32_t---which are only present if
the hardware supports them directly, then the conversion from
unsigned to signed becomes more difficult as well.)

> It would be much better to supply the address of 'i' to the
> procedure that reads the bytes, something like


> int i = 0;
> myfile.read(&i, sizeof(int));


That doesn't work any better, really.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze 05-16-2008 09:33 PM

Re: converting char to int (reading from a binary file)
 
On 16 mai, 23:16, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> itdevries wrote:
> > On May 16, 8:36 pm, sebastian <sebastianga...@gmail.com> wrote:
> >> basically:


> >> int i = *( ( int * )ptr )


> > thanks for your response, it does the trick...


> Be aware that depending on your OS this may break at times,
> not at others, or always work. It depends on your OS mainly
> and if it requires intergers to by specifcally byte aligned.
> I know that this will work on Windows systems fine. I
> understand that wrong alignment it will break on Sun systems.


> If this is platform specific for you and you will never run it
> on another platform and you're sure that your system won't
> break on byte misalligned integers it should be fine to use.
> If you ever plan on running the code on another system then
> you'll need to do it another way.


It will also fail on an Intel if the int's are in the standard
Internet format. In general, you can only count on it working
if you are reading and writing from the same run of the same
program---I've seen cases where just recompiling with a newer
version of the compiler made it fail.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

sebastian 05-16-2008 11:26 PM

Re: converting char to int (reading from a binary file)
 
> That doesn't work any better, really.

that's because istream::read expects a pionter to char (you must cast
it). but as others have already pointed out, there are many problems
with these sorts of casts. there are serialization libraries available
(such as boost::serialize) designed specifically for this purpose, in
case you really want to get the job done right...

coal@mailvault.com 05-17-2008 01:00 AM

Re: converting char to int (reading from a binary file)
 
On May 16, 5:26*pm, sebastian <sebastianga...@gmail.com> wrote:
> > That doesn't work any better, really.

>
> that's because istream::read expects a pionter to char (you must cast
> it). but as others have already pointed out, there are many problems
> with these sorts of casts. there are serialization libraries available
> (such as boost::serialize) designed specifically for this purpose, in
> case you really want to get the job done right...


I agree B.Ser will produce correct results in this case, but it may
not
produce those results efficiently - http://webEbenezer.net/comparison.html

Brian Wood
Ebenezer Enterprises
www.webEbenezer.net


All times are GMT. The time now is 11:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57