Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
convert a float in 4 number uint8

Subject: convert a float in 4 number uint8

From: TheGreatVolpe

Date: 24 Oct, 2013 09:43:08

Message: 1 of 3

hi,

i'm trying to convert a float number (32-bit) in four uint8.
i searched in internet but have not found anything that solves the problem.
Two example:

X=-999
x convert in 4 uint8: 0 192 121 196
or
X=-0.934
x convert in 4 uint8: 75 40 110 191

can someone help me find a function that does this?

thanks

FF

Subject: convert a float in 4 number uint8

From: John D'Errico

Date: 24 Oct, 2013 12:23:07

Message: 2 of 3

"TheGreatVolpe" wrote in message <l4aq3c$jt$1@newscl01ah.mathworks.com>...
> hi,
>
> i'm trying to convert a float number (32-bit) in four uint8.
> i searched in internet but have not found anything that solves the problem.
> Two example:
>
> X=-999
> x convert in 4 uint8: 0 192 121 196
> or
> X=-0.934
> x convert in 4 uint8: 75 40 110 191
>
> can someone help me find a function that does this?

Why not apply num2hex?

Then convert the pairs of hex digits into uint8.

John

Subject: convert a float in 4 number uint8

From: Steven Lord

Date: 24 Oct, 2013 13:58:11

Message: 3 of 3


"TheGreatVolpe " <federicofalorni@hotmail.it> wrote in message
news:l4aq3c$jt$1@newscl01ah.mathworks.com...
> hi,
>
> i'm trying to convert a float number (32-bit) in four uint8.
> i searched in internet but have not found anything that solves the
> problem.
> Two example:
>
> X=-999
> x convert in 4 uint8: 0 192 121 196

TYPECAST.

http://www.mathworks.com/help/matlab/ref/typecast.html

typecast(single(-999), 'uint8')

> or
> X=-0.934
> x convert in 4 uint8: 75 40 110 191

Check your conversion. When I reverse it, I receive:

typecast(uint8([75 40 110 191]), 'single')
ans =

   -0.9303

Changing the endian-ness of the result with SWAPBYTES doesn't resolve the
difference between your X and the uint8 vector you said is your expected
result.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us