×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to convert float to four uint8 values in Matlab?

How to convert float to four uint8 values in Matlab?

How to convert float to four uint8 values in Matlab?

(OP)
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

RE: How to convert float to four uint8 values in Matlab?

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.

RE: How to convert float to four uint8 values in Matlab?

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.

RE: How to convert float to four uint8 values in Matlab?

(OP)
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
% http://de.wikipedia.org/wiki/Gleitkommazahl
% http://www.jsch-online.de/documents/teaching/computer_architecture/ws_200
% 4/gleitkomma_sw.pdfhttp://www.jsch-online.de/documents/teaching/computer_
% 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources