Mail Archives: djgpp/1998/04/21/09:01:51
From: | Vik Heyndrickx <Vik DOT Heyndrickx AT rug DOT ac DOT be>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Separating two 8bit numbers from 16bit
|
Date: | Tue, 21 Apr 1998 14:33:57 +0200
|
Organization: | University of Ghent, Belgium
|
Lines: | 42
|
Message-ID: | <353C9235.51E1@rug.ac.be>
|
References: | <6hi14m$e2o$1 AT hiisi DOT inet DOT fi>
|
NNTP-Posting-Host: | eduserv1.rug.ac.be
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Henri Ossi wrote:
> Now I'm trying to store a 16bit number as two ASCII characters to my file.
> So, I was wondering, if there's a fast way to separate a 16bit number to two
> 8bit numbers?
>
> I know, that I can do this easily with inline asm (and I also can do it),
> but is there a fast and short way to do this in C?
If it has to be portable (my pref.):
unsigned short w;
unsigned char b0, b1;
w = value;
b0 = w % 256;
b1 = w / 256;
dowrite (b1);
dowrite (b2);
If it doesn't have to be portable to big endian machines:
union
{
unsigned short w;
unsigned char b[2];
} u;
u.w = value;
dowrite (u.b[0]);
dowrite (u.b[1]);
The second method is slightly faster.
Note: don't write these characters to a text file but to a binary file.
This question actually belongs in comp.lang.c
--
\ Vik /-_-_-_-_-_-_/
\___/ Heyndrickx /
\ /-_-_-_-_-_-_/
- Raw text -