By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Converting 4-byte char array to float

P: n/a
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

Aug 7 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Gr************@gmail.com wrote:
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.

Aug 7 '06 #2

P: n/a
Gr************@gmail.com wrote:
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?
Aug 7 '06 #3

P: n/a
Gr************@gmail.com schrieb:
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?

--
Thomas
Aug 7 '06 #4

P: n/a
Matt wrote:
Gr************@gmail.com wrote:
>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?
I have never seen anything but 4 byte floats.
Are you confused about float verses double ????
Aug 7 '06 #5

P: n/a
Sjouke Burry wrote:
I have never seen anything but 4 byte floats.
Are you confused about float verses double ????
Uh oh. You're right.
Aug 7 '06 #6

P: n/a
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);
}

--

Frederick Gotham
Aug 7 '06 #7

P: n/a
Thanks, memcpy() worked!
-Greg
Frederick Gotham wrote:
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);
}

--

Frederick Gotham
Aug 8 '06 #8

P: n/a
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".

--

Frederick Gotham
Aug 8 '06 #9

P: n/a
Gr************@gmail.com wrote:
Thanks, memcpy() worked!
See below.
Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.htm>
Aug 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.