Click to See Complete Forum and Search --> : cyping float into char array( or byte array )
venAdder
September 6th, 2006, 05:50 PM
Hi,
I want to copy say 100 floats and store them as char array.
This is what I have in mind, considering VC++ MSDN says on 32 bit flaot is 4 bytes and char is 1 byte
char *temp = ( char* ) &floatVal;
char arr[ 20 ];
for( int i = 0; i < 4; i++ ){
arr[ i ] = temp++;
}
i don't even know if this will work.
can memcpy do this kinda thing?
like memcpy( arr, &floatval, sizeof( float ) );
I r4eally have no idea how to approach this.
basically the problem is I want a char array of floating point values.
VladimirF
September 6th, 2006, 07:02 PM
I want to copy say 100 floats and store them as char array.
...
i don't even know if this will work.
Yes, this will work
can memcpy do this kinda thing?
Yes
like memcpy( arr, &floatval, sizeof( float ) );
Yes, just like that
basically the problem is I want a char array of floating point values.
That I am not sure of. Floating point values requires 4 bytes, as you noted. So what is that "char array of floating point values"?
greg_dolley
September 7th, 2006, 03:40 AM
I think VladimirF (previous post) assumed you wanted to store the floating point _bytes_ into a char array, but I think what you're asking is: how do you convert the _digits_ themselves to a string. Ex: you have a float of 1.25 and you want that number to turn into a four character string, such as "1.25"
Am I correct?
If so, read on. Here's code to convert 100 floats to 100 strings:
long i;
char temp[1024];
char my_strings[100][1024];
float my_floats[100];
memset(temp, 0, sizeof(char)*1024); // clear the temp string
memset(my_strings, 0, sizeof(char)*100*1024); // clear the result strings array
memset(my_floats, 0, sizeof(float)*100); // clear the float array
// initialize the 100 floats array with some values
for(i = 0; i < 100; i++)
{
my_floats[i] = (((float)i)*(float)5.25);
}
// convert the floats to strings
for(i = 0; i < 100; i++)
{
sprintf(temp, "%f", my_floats[i]);
strcpy(my_strings[i], temp);
}
-Greg Dolley
Mitsukai
September 7th, 2006, 05:58 AM
char *temp = ( char* ) &floatVal;
this piece of code is sufficient to enough to get each byte from a float.
however if you want to convert float to a string i do not suggest gred_dolley's code
NMTop40
September 7th, 2006, 06:03 AM
There is no need to use sizeof(char) which is always 1 but you should use sizeof(float) rather than hard-coding in a 4.
floats are POD types so memcpy is legal. You can also assume that an array of floats will be stored in contiguous memory with no padding.
Copying from a char array to an array of floats is also safe, but beware of simply casting a char array back to a float or an array of floats because you will need to ensure correct alignment and the char array might not be correctly aligned (if it is simply a copy of your original array).
venAdder
September 7th, 2006, 11:34 AM
thanks a lot for replies guys. Well the purpose of this was to come up with some kind of serialization to send data over Winsock2. but then I realized how lill i knew abt this, so wanted to know how to get each byte in an array of floats.
teh responses gave me a good explaination , thnx for the help.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.