473,883 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 30536
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_cas t 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_cas t<float&>(*p);
}

float const &Float(char unsigned const *const p)
{
return reinterpret_cas t<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,s izeof 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_cas t<float&>(*p);
}

float const &Float(char unsigned const *const p)
{
return reinterpret_cas t<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,s izeof 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.c om/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
2470
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 but my .dsr file that i created using crystalReport is not migrated , when i try to convert it into .rpt file using add new item in Vb.net Envs it give me the error in the log file saying,
29
3923
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 would you recommend? The w3c HTML validator mentions XHTML 1.0 Transitional, Basic, Strict, and XHTML 1.1. Would I be able to make my existing CSS work in the XHTML page without modification to the .css file?
5
2532
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 applications. I am testing an upgrade of all of the sites and have converted the main root site...although not necessarily fixed any issues. I move on instead and converted one of the virtual roots that is a seperate
3
2435
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 myself. Any help much appreciated.
2
6206
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 process. I narrowed it down to the conversion to IPictureDisp by first loading and saving the icon as a bitmap with no problems; and then repeating the process but adding the step of converting to an IPictureDisp as well. The first operation...
12
3206
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
3327
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. I understand there are some data format differences in text field types and date types that need to be addressed if converting from Jet to Mysql. But other than things that have to be addressed when converting, what other changes must I make?...
7
2504
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 answers to. First, we have a lot of UserControls. I am getting the error "Type uc_mc_btn_footer is not defined." where uc_mc_btn_footer is the name of the UserControl. Also, on almost all of our UserControls the conversion has remmed out all my code...
2
5876
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 ...... Deadlocked Statement: Type : Dynamic Operation: Execute
16
5463
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 learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11109
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7959
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.