Converting 4-byte char array to float

G

Gregory.A.Book

I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?

Thanks,
Greg Book
 
R

red floyd

I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?

Thanks,
Greg Book

assuming that a float is four bytes in your implementation, look up
memcpy() in your reference manual.
 
M

Matt

I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?

You shouldn't be calling them sets if they are arrays.

You will have to find out (by looking at the sourced code for your
library, it would seem) how the data is converted from 4-byte floats to
an array of unsigned chars. Then you will need to use a routine that
inverts the process.

You may have to consider whether the same compiler was used to build
your library as you will use to build the inverter. I don't believe
many compilers have 4-byte floats.

We would hope that whoever wrote your library has already written an
inverter. Have you looked into that?
 
T

Thomas J. Gritzan

I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?

When the library actually stores arrays of 'float's, but gives you a
pointer of type unsigned char*, you could simply reinterpret_cast the
pointer to float*.

Doesn't the library has a documentation with some usage examples?
 
S

Sjouke Burry

Matt said:
You shouldn't be calling them sets if they are arrays.

You will have to find out (by looking at the sourced code for your
library, it would seem) how the data is converted from 4-byte floats to
an array of unsigned chars. Then you will need to use a routine that
inverts the process.

You may have to consider whether the same compiler was used to build
your library as you will use to build the inverter. I don't believe
many compilers have 4-byte floats.

We would hope that whoever wrote your library has already written an
inverter. Have you looked into that?

I have never seen anything but 4 byte floats.
Are you confused about float verses double ????
 
F

Frederick Gotham

posted:
I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?

Thanks,
Greg Book


Here's one method, but the array must be suitably aligned for it to work.

float &Float(char unsigned *const p)
{
return reinterpret_cast<float&>(*p);
}

float const &Float(char unsigned const *const p)
{
return reinterpret_cast<float const&>(*p);
}

void FuncWantsFloat(float) {}

int main()
{
char unsigned arr[sizeof(float)] = {};

FuncWantsFloat( Float(arr) );
}

(Note that no copy is made -- the char array is simple accessed
differently. The float will have the same lifetime as the char array.)

If the char array is NOT suitably aligned, you could always do something
like:

#include <cstring>
using std::memcpy;

float Float(char unsigned *const p)
{
float val;

memcpy(&val,p,sizeof val);

return val;
}

int main()
{
char unsigned arr[4] = {};

Float(arr);
}
 
G

Gregory.A.Book

Thanks, memcpy() worked!
-Greg


Frederick said:
posted:
I am interested in converting sets of 4 bytes to floats in C++. I have
a library that reads image data and returns the data as an array of
unsigned chars. The image data is stored as 4-byte floats. How can I
convert the sets of 4 bytes to floats?

Thanks,
Greg Book


Here's one method, but the array must be suitably aligned for it to work.

float &Float(char unsigned *const p)
{
return reinterpret_cast<float&>(*p);
}

float const &Float(char unsigned const *const p)
{
return reinterpret_cast<float const&>(*p);
}

void FuncWantsFloat(float) {}

int main()
{
char unsigned arr[sizeof(float)] = {};

FuncWantsFloat( Float(arr) );
}

(Note that no copy is made -- the char array is simple accessed
differently. The float will have the same lifetime as the char array.)

If the char array is NOT suitably aligned, you could always do something
like:

#include <cstring>
using std::memcpy;

float Float(char unsigned *const p)
{
float val;

memcpy(&val,p,sizeof val);

return val;
}

int main()
{
char unsigned arr[4] = {};

Float(arr);
}
 
F

Frederick Gotham

posted:
Thanks, memcpy() worked!
-Greg


I only used memcpy because I'm not familiar with most of the C++ Standard
Library. If I'm not mistaken, there's more efficient methods of copying an
array than "memcpy".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top