From: "Campbell, Rolf [SKY:1U32:EXCH]" Newsgroups: comp.os.msdos.djgpp Subject: Re: From Bytes to Int and Char Date: Mon, 16 Aug 1999 11:19:37 -0400 Organization: Nortel Networks Lines: 30 Message-ID: <37B82C09.AAF0D5ED@americasm01.nt.com> References: <37B466D7 DOT 958F09E5 AT americasm01 DOT nt DOT com> <7p7q0m$adb$1 AT solomon DOT cs DOT rose-hulman DOT edu> NNTP-Posting-Host: bmerhc00.ca.nortel.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.7 [en] (X11; I; HP-UX B.10.20 9000/712) X-Accept-Language: en To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Damian Yerrick wrote: > > No. Here's the right way to do it. This code is not edian safe. > > sz = *(short*)(file_buffer+6); > > /* That still isn't byte-order safe. The byte-order-safe way to do it is by > shifting each individual byte. The following code works on all platforms, > but make sure that file_buffer points to _unsigned_ char data or you'll get > nasty sign extension that screws this up. */ > > /* if file was written in big endian (Motorola-order) format */ > sz = ((short)file_buffer[6] << 8) + file_buffer[7]; > > /* if file was written in little endian (Intel-order) format */ > sz = ((short)file_buffer[7] << 8) + file_buffer[6]; > This should clear things up. But, that requires doing a whole bunch of #ifdef's to determine if the current compiler uses BIGENDIAN or not. The easiest way would be to use the lton() function (local to network) which changes the local byte-order to network-byte-order (big-endian). I'm not sure if I have the function name correct, but it is usually associated with TCP/IP stuff and comes with libsocket for DJGPP. This way, all of the byte-order conversion is already done for you and you don't have to worry about your platform. -- -Rolf Campbell (39)3-6318