www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/21/08:46:52

From: Weiqi Gao <weiqigao AT a DOT crl DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Separating two 8bit numbers from 16bit
Date: Tue, 21 Apr 1998 07:33:44 -0500
Organization: Spectrum Healthcare Services
Lines: 22
Message-ID: <353C9228.4A6A0300@a.crl.com>
References: <6hi14m$e2o$1 AT hiisi DOT inet DOT fi>
NNTP-Posting-Host: a116007.stl1.as.crl.com
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?

Short answer:
  short c;
  char hi, lo;
  hi = c/256;
  lo = c % 256;

Shorter answer:
  fprintf(fh, "%d", c);
will save your 16-bit number as two ASCII chars.

--
Weiqi Gao
weiqigao AT a DOT crl DOT com

- Raw text -


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