Skip to main content
gamedev.net
🔒 Locked

float to byte array?

Started by barazor Dec 30, 2001 at 12:46 AM 4 replies 550+ views
Original Post
barazor
barazor
is it possible to convert a float or double to an array of bytes and vice versa?
Shannon Barber
Shannon Barber
What would you like in those bytes?

The x86 op code fbstp will convert a floating point type into an array of packed BCDs.

Edited by - Magmai Kai Holmlor on December 30, 2001 5:03:11 AM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
barazor
barazor
bleh...i was hoping to be able to do it without asm...what im used to doing with integers is to just shift the bits and put it into a byte. since you cant do this with a float, im trying to find a way.
Null and Void
Null and Void
Example 1:
  
union {
float F;
int I;
} MyIntFloat;

Whatever you set F to, I shares the same memory, so you can treat it like an integer. You could also do "unsigned char B[4];" instead of the integer (remember that x86 CPU''s are little endian). Example 2:
  
float F;
int I = *((int *)&F);

I now equals the data within F. There are more methods, but they aren''t as good as those two.

[Resist Windows XP''s Invasive Production Activation Technology!]
a person
a person
what are you trying to do this for? you can do this without asm. just look up the IEEE spec for floats and doubles. its just a matter of following the spec. you can use arrays.

example PSEUDO code:

    
unsigned char floatArray[sizeof(float)];
float theFloat;

// now you can manipulate by:

memcpy(floatArray, &theFloat, sizeof(float));
// or if you are brave use type casting (a bit messy)

((unsigned char *)(&theFloat))[idx];


just remeber floating point numbers have a format to follow. so make sure you look it up. it owuld be best to explain what you need this for.

you can do the same with doubles

Edited by - a person on December 30, 2001 6:20:16 PM
barazor
barazor
thanks guys, this is just what i was looking for

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.