You are here:

C/Converting Bytes to Float

Advertisement


Question
QUESTION: I have an array of 7200 bytes. I need to start with byte0 and use 4 contigous bytes, convert to float then do the next four etc. Can you help. I need to do this in ANSI C.

ANSWER: The simplest thing to do would be to take your array and cast it as a float*, then you're accessing the data as an array of 1800 floats.  For example:

float* floatArray = (float*)byteArray;

You can access each set of 4 bytes simply by indexing the new array:

floatArray[0]; // Bytes 0 - 3
floatArray[1]; // Bytes 4 - 7

If this is not an acceptable solution, let me know.

---------- FOLLOW-UP ----------

QUESTION: Do I need to declare floatArray[1800] above? I get errors when i try to compile.

Answer
In C, you do have to declare all variables at the beginning of a block.  Alternatively, if you don't want the extra few bytes sitting around for the float*, you can just cast when you need to, such as:

((float*)byteArray)[0] = 3.141592f; // access and write a value into the first 4 bytes.

That can be tedious to write out, and potentially error prone, but it will work.  If you don't want to cast the code every time you need to manipulate the data as floats, then the following will do the trick:

int main()
{
  char byteArray[7200] = { 0 };
  float* floatArray = (float*)byteArray;

  // Do all initialization of the byte array

  floatArray[0] = 12.275f;
  floatArray[1] = 9.224f;

  return 0;
}

This tiny little program will create the 7200-byte array (assuming char's are bytes, which they are in my case), fill it with 0's, and will keep a pointer around that interprets the byte array as a floating point array.  If you simply ran this program as written and put a breakpoint on the return, you would see that the first 8 bytes of the byteArray now have non-zero values, because they have been written into as a floating point number.

As always, let me know if you need further assistance.

[APPENDED RESPONSE FOLLOWUP:]
In the case you mentioned in your comments, the array is originally declared as:

BYTE byteRecieve[bytesize];

You can simply use the byteReceive as the source array.  There is no need to create another array of chars to copy the data into.  So you can simply declare your float pointer as:

float* floatArray = (float*)byteReceive;

Let me know if you have further questions/issues.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.