Path: news.mathworks.com!not-for-mail
From: Tom Bryan <tom.bryan@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Fixed point number conversion
Date: Wed, 06 May 2009 13:58:15 -0400
Organization: The MathWorks, Inc.
Lines: 65
Message-ID: <gtsj3o$cc4$1@fred.mathworks.com>
References: <663d3f25-8454-44c2-9b72-bfecfeddd9ed@y10g2000prc.googlegroups.com>
NNTP-Posting-Host: tbryan-maci.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1241632696 12676 144.212.110.200 (6 May 2009 17:58:16 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 6 May 2009 17:58:16 +0000 (UTC)
User-Agent: Thunderbird 2.0.0.21 (Macintosh/20090302)
In-Reply-To: <663d3f25-8454-44c2-9b72-bfecfeddd9ed@y10g2000prc.googlegroups.com>
Xref: news.mathworks.com comp.soft-sys.matlab:537969


Create a fi object with any value (say, 0), and the numerictype that you 
want, then load the value in hex as a string using the "hex" property, 
like this:

 >> a = fi(0, 1,32,29);
 >> a.hex = 'e0000000'

a =

     -1

           DataTypeMode: Fixed-point: binary point scaling
             Signedness: Signed
             WordLength: 32
         FractionLength: 29

 >> a.hex = '6487ed51'

a =

     3.1416

           DataTypeMode: Fixed-point: binary point scaling
             Signedness: Signed
             WordLength: 32
         FractionLength: 29

Best wishes,
Tom Bryan


Sheng wrote:
> Hi
> 
> I have a problem about fixed point conversion.
> 
> I know that use fi toolbox I can convert a dec. number into a hex.
> format, like this
> 
>>> format hex
>>> fi(pi, 1,32,29)
> 
> ans =
> 
> 6487ed51
> 
>           DataTypeMode: Fixed-point: binary point scaling
>                 Signed: true
>             WordLength: 32
>         FractionLength: 29
> 
>              RoundMode: nearest
>           OverflowMode: saturate
>            ProductMode: FullPrecision
>   MaxProductWordLength: 128
>                SumMode: FullPrecision
>       MaxSumWordLength: 128
>          CastBeforeSum: true
> 
> But how do I convert back from 6487ed51 to pi?
> For positive number, I can do hex2dec('6487ed51')/2^29, result is pi.
> But when the number is negative, the hex number is 2's complement, so
> I can't use hex2dec.
> 
> Any suggestion?