From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,bb791bffed99d85a X-Google-Attributes: gid103376,public From: stt@henning.camb.inmet.com (Tucker Taft) Subject: Re: Help with type conversion needed Date: 1996/01/08 Message-ID: #1/1 X-Deja-AN: 134548771 sender: news@inmet.camb.inmet.com (USENET news) x-nntp-posting-host: henning.camb.inmet.com references: <4cnivs$m1k@flood.weeg.uiowa.edu> organization: Intermetrics, Inc. newsgroups: comp.lang.ada Date: 1996-01-08T00:00:00+00:00 List-Id: Nate Bohlmann (njb@res.eng.uiowa.edu) wrote: : Tucker Taft (stt@henning.camb.inmet.com) wrote: : : Unchecked conversion is the reliable way to convert between : : a 16-bit signed and 16-bit unsigned value. This is entirely : : portable, and safe so long as both signed and unsigned subtypes : : are unconstrained (i.e. cover the full range of values). : I'm not sure what you mean here.. Obviously unchecked conversion would : preserve the bit patterns but that's not where I'm having problems. : Say I have some types as follows: : type SignedInt is range -32768..32767; : for SignedInt'Size use 16; : type UnsignedInt is mod 65536; : for UnsignedInt'Size use 16; : Assume the SignedInt is coming off an A/D converter and I want to use : unsigned values in my calculations and I can't convince the A/D converter : to return unsigned values. I believe that in order to keep relative : values correct, I would need to add 32768 to the SignedInt variable to : get a proper value for the UnsignedInt variable. "... to keep relative values correct ..." This seems to be a critical requirement, which I didn't understand before. I presumed you wanted to simply interpret the bits as an unsigned number. In any case, if you have 32-bit ints, then your problem is easy. Just convert to a 32-bit signed int, add 32768, and convert to the 16-bit unsigned type. However, if you can't do that, then use unchecked conversion to convert to the unsigned type, and then add (or subtract) 32768 using unsigned modular arithmetic, which wraps around. : ... How do I go about doing : this properly and portably? The added twist of course is that the SignedInt : is in little-endian format and I'm working on a big-endian machine. I've : solved the twist ... This is presumably done with unchecked conversion, or multiply and add. If so, then just convert directly to the unsigned modular type, and then add 32768. There is no need to ever go through a signed type; that is what is creating your problems. Use unsigned types exclusively if you need to do this kind of biasing or bit/byte-level fiddling. You won't get any overflows. As soon as you use a signed type, you can run into overflow problems when trying to do biasing, etc. : Thanks again.. : Nathan Bohlmann preferred-> njb@res.eng.uiowa.edu -Tucker Taft stt@inmet.com