Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hex to IEEE single precision conversion

763 views
Skip to first unread message

Kevin Bergman

unread,
Jan 7, 2002, 1:26:43 PM1/7/02
to
MATLAB's hex2num command converts a hex value to the IEEE
double-precision floating-point number it represents.


I would like to convert a hex value to the IEEE single precision (32
bits) floating-point number it represents.


i.e. 449A5225=1234.567


Thanks in advance for your help.
Kevin

Kevin Bergman

unread,
Jan 7, 2002, 3:23:10 PM1/7/02
to
I worte this incorrectly. I would like to convert a IEEE single
precision hex number to its decimal equivalent in MATLAB.


i.e. - 449A5225=1234.567

Monica

unread,
Jan 7, 2002, 3:34:35 PM1/7/02
to
HEX2DEC

-Monica

"Kevin Bergman" <kevin....@boge-na.com> wrote in message
news:eea9...@WebX.raydaftYaTP...

Brian.Farrelly

unread,
Jan 7, 2002, 3:53:11 PM1/7/02
to
Kevin Bergman wrote:
>
> MATLAB's hex2num command converts a hex value to the IEEE
> double-precision floating-point number it represents.
>
> I would like to convert a hex value to the IEEE single precision (32
> bits) floating-point number it represents.
>
> i.e. 449A5225=1234.567

I have functions which can do this, namely

hex2float (hot off the press!) which converts a single hex string
of 8 characters to a 32 bit float contained in a uint32.

float2num which can convert a matrix of 32 bit floats contained in
a uint32 matrix and convert them to double for display and
calculation

num2float which takes a matrix of doubles and converts them to
32 bit floats contained in a uint32 matrix.


Thus

>> format hex; b=num2float(1234.567)

449a5225

>> n=float2num(hex2float('449a5225')) ; sprintf('%8.3f',n)

1234.567

and of course

>> nn=float2num(b) ; sprintf('%8.3f',nn)

1234.567

Using format doesn't display the whole number and format long displays
too much so that you see the roundoff error of the conversion!

I'm sending these to Kevin but if anyone else needs them let me know.


Brian


--
mailto:Brian.F...@nho.hydro.com Norsk Hydro Research Centre
phone +47 55 99 68 74 ((( Postboks 7190
fax +47 55 99 69 70 2oooS N-5020 Bergen
home +47 55 13 78 49 HYDRO Norway

Brian.Farrelly

unread,
Jan 7, 2002, 4:17:31 PM1/7/02
to
Monica wrote:
>
> HEX2DEC

>
> >
> > Kevin Bergman wrote:
> >
> > > I would like to convert a hex value to the IEEE single
> > > precision (32
> > > bits) floating-point number it represents.
> > >
> > > i.e. 449A5225=1234.567


HEX2DEC regards the string as representing the integer 1150964261.

Paul Skoczylas

unread,
Jan 7, 2002, 4:25:40 PM1/7/02
to
"Brian.Farrelly" <Brian.F...@nho.hydro.com> wrote:

> I have functions which can do this, namely
>
> hex2float (hot off the press!) which converts a single hex string
> of 8 characters to a 32 bit float contained in a uint32.
>
> float2num which can convert a matrix of 32 bit floats contained in
> a uint32 matrix and convert them to double for display and
> calculation
>
> num2float which takes a matrix of doubles and converts them to
> 32 bit floats contained in a uint32 matrix.
>
>


What I'm still waiting for is a function which will display, to a
user-sepcified number of digits, a decimal representation of the actual
number that is stored in a variable.

For example, we all know that IEEE double precision is incapable of
accurately storing the number 0.1. When you think you're storing 0.1 in a
variable, Matlab actually stores 3fb999999999999a, which you can see is
truncated (much as you can't write 1/3 in decimal without truncating it:
0.33333...). What I'd like to know is what 3fb999999999999a actually
represents. It's not exactly 0.1 (heck, it probably can't be exactly
written in decimal either), but to 20, 50 or 100 digits, what is it's
decimal representation?

I looked at this a while back and I think it can be done fairly easily with
the symbolic toolbox, but I don't have that, and besides it would be more
valuable if it didn't need any add-on toolboxes.

-Paul

Peter Boettcher

unread,
Jan 7, 2002, 4:31:16 PM1/7/02
to
"Monica" <mon...@mathworks.com> writes:

> HEX2DEC

Re-read the post more carefully and try again.

You might have to roll your own using the source to hex2num and:

http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html


--
Peter Boettcher
MIT Lincoln Laboratory
boet...@ll.mit.edu
(781) 981-5275

Brian.Farrelly

unread,
Jan 8, 2002, 4:18:25 AM1/8/02
to
Paul Skoczylas wrote:
>
> What I'm still waiting for is a function which will display, to a
> user-sepcified number of digits, a decimal representation of the actual
> number that is stored in a variable.
>
> For example, we all know that IEEE double precision is incapable of
> accurately storing the number 0.1. When you think you're storing 0.1 in a
> variable, Matlab actually stores 3fb999999999999a, which you can see is
> truncated (much as you can't write 1/3 in decimal without truncating it:
> 0.33333...). What I'd like to know is what 3fb999999999999a actually
> represents. It's not exactly 0.1 (heck, it probably can't be exactly
> written in decimal either), but to 20, 50 or 100 digits, what is it's
> decimal representation?
>
> I looked at this a while back and I think it can be done fairly easily with
> the symbolic toolbox, but I don't have that, and besides it would be more
> valuable if it didn't need any add-on toolboxes.


Paul,

your particular example is fairly easy but the general case is
trickier without the toolbox. I once made a multiprecision calculation
in basic doing the arithmetic directly on the ascii strings. I will
now probably waste some time doing a similar thing in Matlab!

To get back to the particular case.

Using log2 we find 0.1 = 0.8 * 2^(-3). Well, we could have managed
that without log2.

Normalise the fractional part so that 0.1 = 1.6 * 2^(-4).

The 1 is in the hidden bit of the IEEE representation, the exponent
is taken care of, so we need only worry about the 0.6.

The hex sequence 999999999... would be the infinite precision version
of 9/16 + 9/16^2 + ... or (9/16)*(1 + 1/16 + 1/16 +...) and summing
the series we get (9/16)*1/(1-1/16) = 0.6 as required.

What we have is the rounded version which represents

(9/16)*(1 + 1/16 + ... + 1/16^12) + 1/16^13

the last term coming from the hex a being 10 instead of 9.

Now subtract the infinite series from the rounded version to get the
error in the 0.6 which is

1/16^13-(9/16)*(1/16^13 + 1/16^14 + ...)

which simplifies to (1/16^13)*(2/5)


So 0.1 is represented as (1.6 + 2/(5*16^13))/16 = 0.1+0.4/16^14 =
0.100000000000000005551115123125783 approximately

If I am not mistaken this could be written exactly a decimal since
the denominator contains only 2 and 5 as factors and these are
factors of 10.

Peter Boettcher

unread,
Jan 8, 2002, 9:37:21 AM1/8/02
to
"Brian.Farrelly" <Brian.F...@nho.hydro.com> writes:

> Paul Skoczylas wrote:
>>
>> What I'm still waiting for is a function which will display, to a
>> user-sepcified number of digits, a decimal representation of the actual
>> number that is stored in a variable.
>>
>> For example, we all know that IEEE double precision is incapable of
>> accurately storing the number 0.1. When you think you're storing 0.1 in a
>> variable, Matlab actually stores 3fb999999999999a, which you can see is
>> truncated (much as you can't write 1/3 in decimal without truncating it:
>> 0.33333...). What I'd like to know is what 3fb999999999999a actually
>> represents. It's not exactly 0.1 (heck, it probably can't be exactly
>> written in decimal either), but to 20, 50 or 100 digits, what is it's
>> decimal representation?
>>
>> I looked at this a while back and I think it can be done fairly easily with
>> the symbolic toolbox, but I don't have that, and besides it would be more
>> valuable if it didn't need any add-on toolboxes.
>
>

> your particular example is fairly easy but the general case is
> trickier without the toolbox. I once made a multiprecision calculation
> in basic doing the arithmetic directly on the ascii strings. I will
> now probably waste some time doing a similar thing in Matlab!
>
> To get back to the particular case.
>
> Using log2 we find 0.1 = 0.8 * 2^(-3). Well, we could have managed
> that without log2.
>

> [snip]


>
> So 0.1 is represented as (1.6 + 2/(5*16^13))/16 = 0.1+0.4/16^14 =
> 0.100000000000000005551115123125783 approximately

How about fprintf?

>> fprintf('%.56f\n', .1)
0.10000000000000000555111512312578270211815834045410156250

Paul Skoczylas

unread,
Jan 8, 2002, 10:59:23 AM1/8/02
to
"Peter Boettcher" <boet...@ll.mit.edu> wrote:>
> How about fprintf?
>
> >> fprintf('%.56f\n', .1)
> 0.10000000000000000555111512312578270211815834045410156250

On my computer (Win2K, V5.3):

» fprintf('%.56f\n',0.1)
0.10000000000000001000000000000000000000000000000000000000

It shows that there's a difference between 0.1 and how the computer
represents it, but it's not very good at showing what the difference is.
Why is your fprintf so much better than mine?

-Paul

Brian.Farrelly

unread,
Jan 8, 2002, 1:05:47 PM1/8/02
to
Peter Boettcher wrote:

>
> "Brian.Farrelly" <Brian.F...@nho.hydro.com> writes:
>
> > So 0.1 is represented as (1.6 + 2/(5*16^13))/16 = 0.1+0.4/16^14 =
> > 0.100000000000000005551115123125783 approximately
>
> How about fprintf?
>
> >> fprintf('%.56f\n', .1)
> 0.10000000000000000555111512312578270211815834045410156250

Very nice.I always forget what you can do with fprintf.

I was pretty close though!

Brian.Farrelly

unread,
Jan 8, 2002, 1:19:42 PM1/8/02
to

Paul,

I get the same as you on a PC, Win NT, version 6.0.0.88, R12.

On a Unix machine I get the right answer. I guess Peter is
using Unix.

Is this a bug or a feature?

wsun

unread,
Jan 8, 2002, 1:43:44 PM1/8/02
to
I got what Paul had on my NT4SP5, Matlab R12.1 using fprintf. Same with
my DEC unix machine. Why Peter has different fprintf?

Thanks,
--ws

Peter Boettcher

unread,
Jan 8, 2002, 4:22:09 PM1/8/02
to
wsun <ws...@ford.com> writes:

MATLAB presumeably calls the printf functions provided by the C
library. I am using linux with a recent GNU libc, so I would expect
that is the difference. Good to know this is system-dependent.

Paul Skoczylas

unread,
Jan 8, 2002, 5:21:22 PM1/8/02
to
"Peter Boettcher" <boet...@ll.mit.edu> wrote

>
> MATLAB presumeably calls the printf functions provided by the C
> library. I am using linux with a recent GNU libc, so I would expect
> that is the difference. Good to know this is system-dependent.

Well, I don't think it's so good. Of course, my system gives less "good"
results than yours...

I would like Matlab (or any other program that is built for different
platforms) to give the same results on any platform. I suppose, however,
that the different fprintf's we've seen here all meet the minimum
requirement in the ANSI C or IEEE (or?) standard governing such things.
It's just that the linux version goes that extra mile...

So I'd like to reiterate my request for a Matlab function (which does not
require the symbolic toolbox) which will print out, to a number of digits
given by the user, the decimal representation of the actual number stored in
a particular variable.

I wonder if Peter's fprintf works in all possible cases, or if the 0.1 case
is a nice one... If it does work, it shouldn't be too hard for TMW to
incorporate that version in all copies of Matlab...

-Paul

wsun

unread,
Jan 9, 2002, 7:05:29 AM1/9/02
to

Peter Boettcher wrote:
>
> wsun <ws...@ford.com> writes:
>
> > I got what Paul had on my NT4SP5, Matlab R12.1 using fprintf. Same with
> > my DEC unix machine. Why Peter has different fprintf?
> >
> > Thanks,
> > --ws
> >
> > Paul Skoczylas wrote:
> >>
> >> "Peter Boettcher" <boet...@ll.mit.edu> wrote:>
> >> > How about fprintf?
> >> >
> >> > >> fprintf('%.56f\n', .1)
> >> > 0.10000000000000000555111512312578270211815834045410156250
> >>
> >> On my computer (Win2K, V5.3):
> >>
> >> » fprintf('%.56f\n',0.1)
> >> 0.10000000000000001000000000000000000000000000000000000000
> >>
> >> It shows that there's a difference between 0.1 and how the computer
> >> represents it, but it's not very good at showing what the difference is.
> >> Why is your fprintf so much better than mine?
>
> MATLAB presumeably calls the printf functions provided by the
> C library. I am using linux with a recent GNU libc, so I would expect

^^^^^^^^^^^^

What C library? Doesn't TMW pre-compile these functions and ship with
products? It should
be their responsibility to have same results on such functions, or
inform us if they are
may be platform-dependent.

Thanks,
--ws

Akshay

unread,
Jul 16, 2010, 4:49:05 PM7/16/10
to
Hi there,

I am trying to convert from Hex to a floating point number. For Hex to IEEE Double precision number I use the hex2num function and wanted to know which function can be used to convert from Hex to a float.

Thanks.
Akshay

"Brian.Farrelly" <Brian.F...@nho.hydro.com> wrote in message <3C3AB961...@nho.hydro.com>...

Walter Roberson

unread,
Jul 16, 2010, 5:10:02 PM7/16/10
to
Akshay wrote:

> I am trying to convert from Hex to a floating point number. For Hex to
> IEEE Double precision number I use the hex2num function and wanted to
> know which function can be used to convert from Hex to a float.

Convert the hex to uint8 and then typecast() the uint8 to single.

(Provided that the hex represents a IEEE 754 float. You may also have to
change the order of some of the bytes.)

us

unread,
Jul 16, 2010, 5:23:04 PM7/16/10
to
"Akshay " <akshay_ba...@trimble.com> wrote in message <i1qgk0$3jl$1...@fred.mathworks.com>...

> Hi there,
>
> I am trying to convert from Hex to a floating point number. For Hex to IEEE Double precision number I use the hex2num function and wanted to know which function can be used to convert from Hex to a float.
>
> Thanks.
> Akshay

one of the solutions
- if(f) by FLOAT you mean a SINGLE precision data...

v=pi; % <- a DOUBLE by default...
hv=num2hex(v)
% hv = 400921fb54442d18
dv=hex2num(hv);
isequal(dv,pi)
% ans = 1
sv=single(hex2num(hv));
isequal(sv,single(pi))
% ans = 1
num2hex(sv)
% ans = 40490fdb

us

Walter Roberson

unread,
Jul 16, 2010, 5:35:57 PM7/16/10
to
us wrote:

> one of the solutions
> - if(f) by FLOAT you mean a SINGLE precision data...
>
> v=pi; % <- a DOUBLE by default...
> hv=num2hex(v)
> % hv = 400921fb54442d18
> dv=hex2num(hv);
> isequal(dv,pi)
> % ans = 1
> sv=single(hex2num(hv));
> isequal(sv,single(pi))
> % ans = 1
> num2hex(sv)
> % ans = 40490fdb

num2hex() applied to a single will print out the hex equivalent of the single,
but hex2num() applied to a sequence of hex digits shorter than would be the
case for a double, implicitly pads with hex 0 to reach the size for a double
and converts that. There is no option available to num2hex to tell it to
interpret the hex as a single. This does make a difference as the
representation of a single is not just a shorter version of the representation
of a double.

James Tursa

unread,
Jul 16, 2010, 6:11:04 PM7/16/10
to
"Paul Skoczylas" <pa...@cfertech.com> wrote in message <DhK_7.17$M5....@jekyl.ab.tac.net>...

Looks like this thread has been resurrected, so I will add this reply. See this FEX submission to get the exact decimal equivalent of any single or double number:

http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

James Tursa

us

unread,
Jul 17, 2010, 6:49:10 AM7/17/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <i1qjif$mg3$1...@canopus.cc.umanitoba.ca>...

of course...
but i showed something different...
see also the results of ISEQUAL...

us

Alan Cooper

unread,
Feb 26, 2013, 10:12:08 AM2/26/13
to
"Brian.Farrelly" <Brian.F...@nho.hydro.com> wrote in message <3C3A106B...@nho.hydro.com>...
> Monica wrote:
> >
> > HEX2DEC
> >
> > >
> > > Kevin Bergman wrote:
> > >
> > > > I would like to convert a hex value to the IEEE single
> > > > precision (32
> > > > bits) floating-point number it represents.
> > > >
> > > > i.e. 449A5225=1234.567
>
>
> HEX2DEC regards the string as representing the integer 1150964261.
>
> Brian
>
>
Hi Brian,

I am also looking for a simple way to translate back and forth between 32-bit hex and single precision floating point representation. I would appreciate a copy of your functions.

Thanks in advance,

Alan

James Tursa

unread,
Feb 26, 2013, 5:06:10 PM2/26/13
to
"Alan Cooper" <alan.co...@rohde-schwarz.com> wrote in message <kgijc8$meg$1...@newscl01ah.mathworks.com>...
>
> I am also looking for a simple way to translate back and forth between 32-bit hex and single precision floating point representation. I would appreciate a copy of your functions.

Assuming you have a hex string to start with and it represents an IEEE single precision bit pattern:

h = your hex string;
s = typecast(uint32(hex2dec(h)),'single');

Depending on which machines are involved, you might also need to use the swapbytes function on s to account for little endian vs big endian differences.

James Tursa
0 new messages