views:

115

answers:

2
+1  Q: 

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.

+1  A: 
// Converts a single group of 4 chars to 1 float.
float char_to_float(const char* cs)
{
    return *(float*)cs;
}

// Converts every 4 chars into one float. len is the number of floats to unpack.
void chars_to_floats(const char* restrict chars, float* restrict floats, size_t len)
{
    for(size_t i = 0; i < len; ++i)
    {
        *floats = char_to_float(chars);
        *chars += 4;
        *floats += 1;
    }
}
Clark Gaebel
nice! thanks. I am new to C and I did not realize I could just cast to a different pointer type and C would know what to do.
juxstapose
No problem, bro. :)
Clark Gaebel
+1  A: 

"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;
}
This only works if `chars` is correctly aligned for access as a `float`. You can fix that by using `memcpy(floats++, fp++, sizeof *fp);` instead of the assignment.
caf