Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: reading binary file into memory. Converting from char to uint32, float, double, ASCII strings etc (static_cast< > ?) Date: Sun, 16 Oct 2011 10:45:49 +1300 Lines: 38 Message-ID: <9fud8dFvblU6@mid.individual.net> References: <2508ba10-93ae-4864-bb07-dc418eae066b@g16g2000yqi.googlegroups.com> <9fu8pkFvblU5@mid.individual.net> <4ecf2948-8536-44e4-8226-2a0f339ece27@z26g2000yqh.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net oypLd0kfI7+qwqpX+Dol7g7QVDxsPWM92axJG3i+L08tB1ctiP Cancel-Lock: sha1:HUmqTlz+pd6SG8tvSz/gQqFICR4= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: <4ecf2948-8536-44e4-8226-2a0f339ece27@z26g2000yqh.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:10869 On 10/16/11 10:17 AM, someone wrote: > On 15 Okt., 22:29, Ian Collins wrote: >> On 10/16/11 08:00 AM, someone wrote: > >>> int R1_begin; >>> R1_begin = static_cast (memblock[0]);//, 1, >>> 'uint32',endianNess); >> >> You can't just cast an array of char to an int and expect the right >> result (unless the data is written in the same byte order as your machine). > > So how should I do it? > [to begin with, we can just assume that the data is written in the > same byte order and see if that works...] > >> The most convenient way to convert the byte order is to use the network >> ordering functions (if your platform has them). See ntohl. > > Sorry, I have absolutely no idea about any network functions... > I was (pretty) sure that somebody had some code they could show here? > This "ntohl" looks a bit confusing to me - besides that, it's not > standard C++. They are typically macros, just look them up if you want to see how they work. >>> And what about big endian / little endian? I'm confused. Looking >>> forward to hear from you! >> >> Google "endian" > > I did it. But what I meant is, what are your thoughts about it, when > reading a binary file like shown in my code? I use the network byte order functions, that's what they are there for. -- Ian Collins