www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/14/02:11:44

From: Chris Holmes <cholmes AT surfsouth DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: From Bytes to Int and Char
Date: Fri, 13 Aug 1999 23:17:37 -0400
Organization: Georgia Institute of Technology, Atlanta GA, USA
Lines: 34
Message-ID: <37B4DFD1.4D66@surfsouth.com>
References: <rfXs3.4$bZ1 DOT 1603 AT typhoon01 DOT swbell DOT net> <37B466D7 DOT 958F09E5 AT americasm01 DOT nt DOT com>
NNTP-Posting-Host: r33h43.res.gatech.edu
Mime-Version: 1.0
X-Trace: news-int.gatech.edu 934601297 16174 128.61.33.43 (14 Aug 1999 03:28:17 GMT)
X-Complaints-To: usenet AT news DOT gatech DOT edu
NNTP-Posting-Date: 14 Aug 1999 03:28:17 GMT
X-Mailer: Mozilla 3.04Gold (Win95; I)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Campbell, Rolf [SKY:1U32:EXCH] wrote:
> 
> Nick wrote:
> 
> > I am trying to load some data from a files, the 6 and 7th bytes are an
> > Integer and so are the 11th and 12th.  How do I make them into an INT?  This
> > is what I used earlier
> >
> > char ver, type;
> > int sz, tz;
> >
> > sz = file_buffer[6] + file_buffer[7];
> > is that right?
> 
>     No.  Here's the right way to do it.  This code is not edian safe.
> 
> sz = *(short*)(file_buffer+6);
 
  (sorry... have to say it) They aren't endians, they're native 
arrangements.  Please, be PC with your PC.  <grin>

  One solution (kinda tricky because you need to make sure they are 
stored on a 2 byte boundary or hack around my trick, which is a hack
itself).
  Given: void *buffer;
  make: char *c_buffer=(char *)buffer;
  make: short *i_buffer=(short *)buffer;
  then: short i = i_buffer[BYTE_OFFSET / 2];

  (this should be a little more endian safe)

  Chris


- Raw text -


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