delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/16/16:45:02

From: "Campbell, Rolf [SKY:1U32:EXCH]" <cp1v45 AT americasm01 DOT nt DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: From Bytes to Int and Char
Date: Mon, 16 Aug 1999 11:13:46 -0400
Organization: Nortel Networks
Lines: 31
Message-ID: <37B82AAA.E7223AB1@americasm01.nt.com>
References: <rfXs3.4$bZ1 DOT 1603 AT typhoon01 DOT swbell DOT net> <37B466D7 DOT 958F09E5 AT americasm01 DOT nt DOT com> <37B4DFD1 DOT 4D66 AT surfsouth DOT com> <37B57EB0 DOT 492A93AE AT unb DOT ca> <37ba82f5 DOT 7514501 AT news DOT clara DOT net>
NNTP-Posting-Host: bmerhc00.ca.nortel.com
Mime-Version: 1.0
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

George Ryot wrote:

> > > >     No.  Here's the right way to do it.  This code is not edian safe.
> > > > sz = *(short*)(file_buffer+6);
> Me thinks some programmers just get stuck in hack mode determined to
> produce unsafe code for the sake of a few microseconds. ;-))
>
> How would Nick extract the byte pairs he needs using that last hack?
>
> The following (boring?) solution should do the trick:
>
>     unsigned short sz, tz;
>     sz = (file_buffer[5]  << 8) & file_buffer[6];
>     tz = (file_buffer[10] << 8) & file_buffer[11];
>
> Note that I have assumed you really do mean 6th, 7th, 11th & 12th
> bytes (file_buffer[0] would be the first byte) and that the 16-bit
> integers are stored high byte low byte.  I have also assumed that the
> values are unsigned (always positive), things might get complicated
> otherwise.  You can use int instead of short if you prefer.

    I don't understand how your code is any less of a 'hack' than mine.  Both are not
endian safe.  Both will work consistantly on each individual machine (I'm not saying
that you could exchange data files and have them work).  How is my code 'unsafe'?


--
     -Rolf Campbell (39)3-6318



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019