delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/04/22/02:59:10

From: "Ben Alford" <benjamin DOT alford AT surrey DOT uni DOT ntl DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Reading in Data
Lines: 24
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
Message-ID: <a95M4.1810$In2.35324@news6-win.server.ntlworld.com>
Date: Sat, 22 Apr 2000 00:05:24 +0100
NNTP-Posting-Host: 212.250.68.32
X-Complaints-To: abuse AT net DOT ntl DOT com
X-Trace: news6-win.server.ntlworld.com 956358342 212.250.68.32 (Sat, 22 Apr 2000 00:05:42 BST)
NNTP-Posting-Date: Sat, 22 Apr 2000 00:05:42 BST
Organization: ntl News Service
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi

I am trying to read data from a file. The file is in binary format. The data
is either 1,2 or 4 bytes.
I need to convert the byte information into an integer value.
The technique that I have tried for reading a field of 2 bytes would be,

char twobyte[3]

 fread(twobytes, sizeof(twobytes), 1, FILE)
strcpy(twobytes, twobytes, 2)
 twobyte[2] = '\0'
 num = (int)(unsigned char)twobyte[1]*256 + (int)(unsigned char)twobyte[0]

This sort of works but feels nasty. It doesn't work for values that are
negative.

Any ideas would be very appreciated

thanks

Ben


- Raw text -


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