473,517 Members | 2,950 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 30478
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
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...
29
3854
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...
5
2497
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...
3
2399
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
6160
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...
12
3131
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
3301
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. ...
7
2479
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...
2
5849
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
5422
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
7295
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7197
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...
0
7430
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. ...
0
7598
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7556
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4786
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...
0
3277
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
833
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.