From: peuha AT cc DOT helsinki DOT fi (Esa A E Peuha) Newsgroups: comp.os.msdos.djgpp Subject: Re: Char/unsigned/unsigned char conversion problems. Date: 19 Feb 1997 09:20:08 GMT Organization: University of Helsinki Lines: 53 Distribution: world Message-ID: <5eegk8$7s0@oravannahka.Helsinki.FI> References: <5e6mqp$6ah AT lion DOT cs DOT latrobe DOT edu DOT au> Reply-To: Esa DOT Peuha AT helsinki DOT fi NNTP-Posting-Host: kruuna-ether.helsinki.fi Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Gregary J Boyles (boylesgj AT lion DOT cs DOT latrobe DOT edu DOT au) wrote: : I am moving characters + text attribute to a window, which is later copied : directly to video RAM. : The window is stored as a 1D array and the text attribute is an unsigned with : the lower byte zeroed. : The function : : void WindowC::Write(char Ch) : { : : Window[Index]=Attribute | (unsigned)Ch; : : } : In one example of using this function I should have seen a light grey : character on a blue back ground. : I.E. 0 001 0111 10110011 : | | | | : | | | +------ASCII 179 : | | +-------------light grey fore ground : | +------------------blue back ground : +---------------------non blinking : Instead I saw a white flashing character on a light grey back ground. : I.E. 0 001 0111 10110011 : | | | | : | | | +------ASCII 179 : | | +-------------white fore ground : | +------------------light grey back ground : +---------------------blinking The binary values here are identical (the latter should be 1 111 1111 ...). : I was assuming that the upper byte would be zeroed in the conversion : (unsigned)Ch. : I.E. : char : 10110011 -----> unsigned : 0000000010110011 : Is this assumption wrong and the upper byte could be anything? It's *not* anything. What happens here is that your (signed) char is first *implicitly* cast to (signed) short and then *explicitly* cast to unsigned short, so the upper byte is all zeros or all ones depending on the highest bit of the lower byte. You should use (short)(unsigned)Ch. -- Esa Peuha student of mathematics at the University of Helsinki http://www.helsinki.fi/~peuha/