This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Convert an array of 4 chars to float32



I am trying to covert an array of chars (arriving from the SCI from ) to float32.

Can anyone help ?

I tried the following code:

unsigned char fArray[4];

float32 fff;

float32 *pArray;

 

fArray[0] = 0x40;

fArray[1] = 0x49;

fArray[2] = 0x0f;

fArray[3] = 0x19;

 

pArray = (float32*)fArray;

fff = *pArray;

I expected to find 0x40490F19 in fff  but the watch window shows 0x00490040.

Thanx, Danny 

  • Hi  Danny,

    If you just want to append the array elements into a single float32, then cant you use Bit-shifting operator (<< 8, << 16) and OR them?

    Regards,

    Sid

  • Hi Sid,

    Thanks for the hint, I tried it with no success. 

     

    After wasting 6 hours I found a hint in the TMS320C28x Optimizing C/C++ Compiler v5.0.0 User's Guide 

    about using      __byte()  and    __mov_byte() intrinsics and this actually solved my problem.

    The following code does the trick :)  

     

    char fArray[4];

    float32 f1 = 3.141592;

    float32 f2 = 0;

     

    // From a float variable (f1) to a chars array (fArray):

    fArray[0] =  __mov_byte((int *)&f1,0);

    fArray[1] =  __mov_byte((int *)&f1,1);

    fArray[2] =  __mov_byte((int *)&f1,2);

    fArray[3] =  __mov_byte((int *)&f1,3);

     

    // From a chars array (fArray) to a float variable (f2):

    __byte((int *)&f2,0) = fArray[0];

    __byte((int *)&f2,1) = fArray[1];

    __byte((int *)&f2,2) = fArray[2];

    __byte((int *)&f2,3) = fArray[3];

     

    Thanks,

    Danny

  • The relevant detail is that char is a 16-bit type on C2000.  See section 6.3, Data Types, in spru514c.

  • The easiest way I've found is to create a union between a bit-field breakout in an int32 struct and a float.  For example:

     

    typedef struct {

    int32 byte0: 8;

    int32 byte1: 8;

    int32 byte2: 8;

    int32 byte3: 8;

    } CVT_BYTES;

     

    typedef union {

    float fp32;

    CVT_BYTES bytes;

    } CVT_32BIT;

     

    then in your function...

    CVT_32BIT chartofloat;

    char bvals[4];

    float fpval;

    bvals[0] = ...

    bvals[1] = ...

    bvals[2] = ...

    bvals[3] = ...

    chartofloat.bytes.byte0 = (int32) bvals[0];

    chartofloat.bytes.byte1 = (int32) bvals[1];

    chartofloat.bytes.byte2 = (int32) bvals[2];

    chartofloat.bytes.byte3 = (int32) bvals[3];

    fpval = chartofloat.fp32;

     

    Keep in mind that you need to account for little endianness (low-word | high-word in memory), too.  Doing a simple union with chars won't work properly because, as noted above, a char occupies 16 bits since the minimum addressable unit is 16 bits on a C2000.  Generally speaking, I also add a 16-bit (CVT_WORDS) struct type as well as an int32 and uint32 types in order to "cast" between any format.

     

    Mark