Search code examples
cprogramming-languagestypestype-conversion

char* to float*


I want to write a function that unpacks a char* buffer to a float* buffer. The char* buffer was originally packed as floats to bytes.

What is the best way to do this?

I have implemented integers below. I have packed an array of 4 byte sized integers in a char* buffer.
This is the function I used to unpack it and assign it to int*.

void CharToInt(int* tar, char* src, int len) {
    char* val = src;
    char* end = src + len-1;
    for( ; val<end; tar++,val+=sizeof(int)) {
            *tar = ( (((*val) < 0) ?   256 + (*val)   : (*val)        ) +
                     (((*val+1) < 0) ? 256 + (*val+1) : (*val+1) << 8 ) +
                     (((*val+2) < 0) ? 256 + (*val+2) : (*val+2) << 16) +
                     (((*val+3) < 0) ? 256 + (*val+3) : (*val+3) << 24) );
    }
}

Adding 256 to a signed byte seems to takes care of the signed char. (-128 to 127).
This is because it is the same as flipping bits and subtracting 1?

Anyway, I am looking for the float* equivalent.


Solution

  • "restrict" is not C89, only C99. sizeof(float) can != 4 on several machines. This solution is independent from sizeof(float), works on all ANSI C environments and returns the number of successful imported floats in "float".

    int chars_to_floats(const char *chars, float *floats, int len)
    {
      int converted = 0;
      float *fp = (float*)chars;
      while( len >= sizeof*fp )
      {
        *floats++ = *fp++;
        len -= sizeof*fp;
        ++converted;
      }
      return converted;
    }