www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/17/15:34:23

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: Tue, 17 Aug 1999 11:16:22 -0400
Organization: Nortel Networks
Lines: 31
Message-ID: <37B97CC6.7E054E7F@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> <37B82AAA DOT E7223AB1 AT americasm01 DOT nt DOT com> <37bd890c DOT 14677884 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:

> "Campbell, Rolf [SKY:1U32:EXCH]" <cp1v45 AT americasm01 DOT nt DOT com> wrote:
>
> >     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'?
>
> Casting between pointers to different types is nearly always unsafe.

    It depends what you mean by 'unsafe'.  If you look at code, I think you would find
that most pointer casts work fine and that a small minority cause unsafe code.


> The code I posted will solve the original problem (as I understand it)
> and will work the same on *all* implementations except attention is
> required to unsigned values.

[your original code]:
 unsigned short sz, tz;
    sz = (file_buffer[5]  << 8) & file_buffer[6];
    tz = (file_buffer[10] << 8) & file_buffer[11];

Your code does solve the original problem.  And you know what... I totally missed the fact
that you were forcing big-endian-ness.  Oh well, I live: I learn...

--
     -Rolf Campbell (39)3-6318



- Raw text -


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