How to decode array of floats from ofBuffer

#1

I’m using ofxLibwebsockets to receive an array of 32-bit floats as binary (JSON is too slow for my usecase).

From ofxLibwebsockets I get an ofBuffer variable that I have saved as buffer:

char *data = (char *)buffer.getData();

int size = buffer.size() / sizeof(float);

for (int i = 0; i < size; i++) {
    std::cout << data[i] << endl;
}

I would expect this to output each float value from the array, but instead it outputs random values like \312. I’m a complete beginner in C++ but after searching like crazy I still cannot find how to do this. Any pointers (pun intended)?

#2

casting the array to float instead of char should work as in:

float *data = (float *)buffer.getData();

int size = buffer.size() / sizeof(float);

for (int i = 0; i < size; i++) {
    std::cout << data[i] << endl;
}

that’s if the two platforms are using the same endianess. though, not sure if libwebsockets already takes care of that but if not and you are in platforms with different endianness then the conversion is more complicated

#3

Thanks, so much, that worked! I thought I had tried that variation already, but apparently not :sweat_smile: