Results 1 to 5 of 5

Thread: float array to string - urgent!!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Location
    NW England
    Posts
    110

    Unhappy float array to string - urgent!!!

    All,
    I have an array of 8 floating point numbers which i need to convert to a string so that i can send it over a network so my VB front end can read it.

    How do i convert the floats to a string??

    Need to get this sorted quick....been struggling with it for 2 days now

    Matt

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    C:
    sprintf
    %f is a floating var.

    C++:
    ostringstream
    in the header <sstream>
    use it like any other stream (cout, ofstream)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Location
    NW England
    Posts
    110
    Im not actually printing them to screen.....i need to convert the floats to one big string and then write them character by character across the network...

    Matt

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    printf prints to the screen.
    sprintf prints to a string


    ostream prints to the stream
    ostringstream prints to a string



    I know what I'm talking about.
    Code:
    #include <sstream>
    using std::ostringstream;
    
    float  arF[10];
    
    void Convert(float *arF, int len, std::string &str)
    {
      ostringstream ss;
      for(float *p = arF; p < arF+len, p++) {
        ss << *p;
      }
      str = ss.str();
    }
    Add formatting options as you wish.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Location
    NW England
    Posts
    110
    I sussed it!!!

    I used sprintf and it solved the problem....sorry for the confusion, i guess it was me not knowing what i am doing!!! Im a hardware engineer after all, not a softy!

    Thanks
    Matt

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