CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2004
    Posts
    391

    cyping float into char array( or byte array )

    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.

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: cyping float into char array( or byte array )

    Quote Originally Posted by venAdder
    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
    Quote Originally Posted by venAdder
    can memcpy do this kinda thing?
    Yes
    Quote Originally Posted by venAdder
    like memcpy( arr, &floatval, sizeof( float ) );
    Yes, just like that

    Quote Originally Posted by venAdder
    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"?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    May 2005
    Location
    United States
    Posts
    263

    Re: cyping float into char array( or byte array )

    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:

    Code:
      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

  4. #4
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: cyping float into char array( or byte array )

    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

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: cyping float into char array( or byte array )

    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).

  6. #6
    Join Date
    Aug 2004
    Posts
    391

    Re: cyping float into char array( or byte array )

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured