Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert float to four uint8 values in Matlab?

Status
Not open for further replies.

condorrr

Mechanical
Mar 18, 2005
4
DE
Hi all,

I'm trying to send a double value (x) from Matlab to a 8bit microcontroller over the PC's serial port, but I dont't know how to make four 8 bit values out of my double. In the µC I can put the four 8bit values together to a 32bit float:

In C++ I would use a union:
union{
uint8_t j[4];
float f;
} u;
where i can selcet with u.f my float or with u.i[1] for example the first byte.

For example:
float 4xuint8
10.5 -> 0 0 40 65

and the reverse conversion
4xint8 float
0 0 40 65 -> 10.5


Can anyone give me an advice how to make this conversion in Matlab?

Regards,
condorrr
 
Replies continue below

Recommended for you

You gave an example in "c". In C++ you would create an alias on a reinterpret_cast.

Your solution in c should be better for a uC.

You must address the endian issue. If your uC compiler has knowledge of double, then you should not have a problem, excpet about endianess and putting the double back together. You also must make sure each machiine has the same definition of what a double is. If they both conform to the IEEE STD then you are OK (again except for endianess). If your PC is a Pentium and your uC is an intelian also, you will be both little endian. If you uC is big endian you will need to convert. There is a simple union trick to change endianess with a union and you may be able to combine this trick with your uitn8 solution into one clever solution.

If you do not get MATLAB, and the PC and the uC to conform to the IEEE double, then you will also have to create your uC double by manipulation inside of MATLAB. That would be bit twiddling.
 
Take a look at the new typecast() function introduced with ML 7.1. What's also neat is that it's a MEX function and they provide the source code for it.
 
Thanks for the advice.
Finally I have found some information about the mathematics behind these conversions. So I have written two small functions to do this work for me:

Function one:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% crate four 8bit values from 32bit float
% % % 4/gleitkomma_sw.pdf% architecture/ws_2004/gleitkomma_sw.pdf
% IEEE 754
% ZG 10.14.05
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [res] = float2int(x)

if x == 0
my32bits = zeros(1,32)
else
B = 127; % Biasing (32 bit float)
S = fliplr( dec2binvec( mysign(x),1) ); %sign

my32bits = horzcat( S, ...
fliplr( dec2binvec((floor(log2(abs(x)))+B),8) ), ...
fliplr( dec2binvec((abs(x)/ (2^(floor(log2(abs(x)))))-1)*2^23, 23)));
end

a = ( binvec2dec( fliplr(my32bits(1:8)))) ;
b = ( binvec2dec( fliplr(my32bits(9:16))));
c = ( binvec2dec( fliplr(my32bits(17:24))));
d = ( binvec2dec( fliplr(my32bits(25:32))));

res = [d, c, b, a];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret] = mysign(x)
if(x)>0
ret=0;
elseif(x)<=0
ret=1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



Function two:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ZG 10.14.05
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [res] = int2float(fourbytes)
my32bits = (horzcat( fliplr(dec2binvec(fourbytes(4),8)), ...
fliplr(dec2binvec(fourbytes(3),8)), ...
fliplr(dec2binvec(fourbytes(2),8)), ...
fliplr(dec2binvec(fourbytes(1),8)) )) ;

S = my32bits(1); %sign
C = my32bits(2:9); %exponent C=0->, C=1
M = my32bits(10:32); %mantisse
M = horzcat(1,M);

res = (-1)^S * sum(2.^-(0:23).*M(1:24)) * 2^(binvec2dec(fliplr(C))-127);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top