473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Converting 4-byte char array to float

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
9 30426
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mustafa | last post by:
Dear sir , I have built my application in visual basic 6.0 and crystal Report8.5 , Now i migrated my application to VB.net using the upgrade wizard.My visual basic form is upgraded to vb.net...
29
by: Armand Karlsen | last post by:
I have a website ( http://www.zen62775.zen.co.uk ) that I made HTML 4.01 Transitional and CSS compliant, and I'm thinking of converting it into XHTML to learn a little about it. Which XHTML variant...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
3
by: Mary | last post by:
Hi, Does anyone know of any software out there that would convert an application written in VBScript to either VB.NET or C#/C++ quite quickly for me, or will I have to re-write the application...
2
by: Map Reader | last post by:
Greetings, I am converting an old VB6 application to use .NET. One of the old controls loads icons from the disk and displays them. However, the transparent color turns to blue somewhere in the...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
4
by: Bob Alston | last post by:
Anyone have experience with converting an access app from Jet database to Mysql? I am specifically looking for any changes I would have to make to my access forms, queries, modules, vba code, etc....
7
by: Coleen | last post by:
Does anyone have any good detailed information on the conversion process? We are in the process of converting 2 projects from 2003 to 2005 and have some conversion errors that I can not find...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.