wxString to double

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
chocobo_ff
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Jan 10, 2006 12:52 am
Location: New Zealand

wxString to double

Post by chocobo_ff »

Hi, this may sound like a stupid question, but is there a function for converting wxString to double, basically what I have is a string of numbers, separated by space, and I want to convert the string into an array of numbers (double), e.g. the string may be something like:

Code: Select all

wxString data = "1 2 3 4 5 6 7 8";
The problem I have is I don't know how to deal with it when there's an unknown number of doubles in the string (e.g. sometimes I could have 1, other times I'll have 10 say)

Thanks in advance for any help :)
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Re: wxString to double

Post by sethjackson »

chocobo_ff wrote:Hi, this may sound like a stupid question, but is there a function for converting wxString to double,
.......
Yes there is, but not sure if it will help you.....

http://www.wxwidgets.org/manuals/2.6.2/ ... ngtodouble
chocobo_ff
Earned a small fee
Earned a small fee
Posts: 17
Joined: Tue Jan 10, 2006 12:52 am
Location: New Zealand

Re: wxString to double

Post by chocobo_ff »

sethjackson wrote:Yes there is, but not sure if it will help you.....
Hi yes I know that function already... thanks for the help anyway :) actually I just found there's a function called AfterFirst that finds the first whitespace (' ') and take a sub string from there, so problem now solved :)
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Re: wxString to double

Post by sethjackson »

chocobo_ff wrote:
sethjackson wrote:Yes there is, but not sure if it will help you.....
Hi yes I know that function already... thanks for the help anyway :) actually I just found there's a function called AfterFirst that finds the first whitespace (' ') and take a sub string from there, so problem now solved :)
Cool. 8)
Arakageeta1
Knows some wx things
Knows some wx things
Posts: 38
Joined: Tue Oct 18, 2005 12:26 am

Post by Arakageeta1 »

You could also use std::stringstream and check the status flags to see if you've read in all the available numbers.
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

What about use a wxStringTokenizer [1] to separate substrings and use wxString::ToDouble [2] to translate on double.


1 : http://www.wxwidgets.org/manuals/2.6.2/ ... gtokenizer
2 : http://www.wxwidgets.org/manuals/2.6.2/ ... ngtodouble
What is little and green, witch go up and down ??
Yoda playing with the force.
ChrisBorgolte
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Dec 14, 2005 8:38 am
Location: Dortmund, Germany
Contact:

Post by ChrisBorgolte »

Based on Arakageeta1 reply I would suggest something like:

std::string s = "1 2 3 4 5 6 8";
std::istringstream sst(s.c_str());
std::vector<double> V;
std::copy(std::istream_iterator<double>(sst), std::istream_iterator<double>(),
std::back_inserter(V));
//output to stdout:
std::copy( V.begin(), V.end(), std::ostream_iterator<double>(std::cout," ") );
std::cout << std::endl;

You can replace std::string s with wxString s.
This is just STL and has the advantage that you can use it even if you dont use wxWidgets.
Note that this method stops parsing at the given token if it can't translate the value to double. You will not get any information (exception) about it.
Arakageeta1
Knows some wx things
Knows some wx things
Posts: 38
Joined: Tue Oct 18, 2005 12:26 am

Post by Arakageeta1 »

I didn't know you could do that with std::copy. Nice.
sobaka
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon Jan 08, 2007 2:43 am

Post by sobaka »

Hi a have a similar problem, I need read from file some data. But I need to skip first lines in file and get amount of lines from “N=”, base on this get these lines. Then founded in number "E=" get the rest lines. Below you may see that I have in file.
And also that I do to get "N=" and numbers "E=".

===File===

Code: Select all


VARIABLES = "X","Y","Z" // < ------- well this I think understandable first column "x" second "y" third "z" this it is need to skip VARIABLES = "X","Y","Z"ZONE T="1",F=FEPOINT
 ZONE T="1",F=FEPOINT,N=   2.00000     ,E=   2.00000     ,ET=BRICK    // <---- N=   2.00000 this is how much lines "0.0000000D+00" like this numbers.
  0.0000000D+00  0.0000000D+00  0.0000000D+00
  0.1000000D+00  0.0000000D+00  0.0000000D+00
      1      2     11     10     28     29     38     37      2      1 // < --------------E=   2.00000 this how much such lines
      2      3     12     11     29     30     39     38      2      1
===Code===

Code: Select all


#include <fstream>
#include <string>
#include <iostream>
#include <strstream>

using namespace std;

inline double parseDouble(ifstream &str)
{
  string token;
  strstream convertor;
  double value;

  str >> token;
  convertor << token.replace(token.find('D'), 1, "E");
  convertor >> value;

  return value;
}

int main()
{

  ifstream src("MESHGL.dat");
  string token, line;
  int numPoints = -1, numElements = -1;
  int pos;

  do {
    src >> token;
    if (token.find("N=") != string::npos) src >> numPoints;
    if (token.find("E=") != string::npos) src >> numElements;
  } while (!src.eof() && (numPoints == -1 || numElements == -1));

  if (!src.eof())
    cout << numPoints << ", " << numElements << '\n';
  else
    cout << "Invalid source file format!\n";

  // skip end of line with "E="
  getline(src, line);

  double point[3];
  int elements[10];

  // start to read N-type lines
  while (numPoints--)
  {
    point[0] = parseDouble(src);
    point[1] = parseDouble(src);
    point[2] = parseDouble(src);

    cout << point[0] << "," << point[1] << "," << point[2] << "\n";

    // skip end of line (just in case of comments)
    getline(src, line);
  }

  // start to read E-type lines
  while (numElements--)
  {
    cout << "E=";
    for (int itemIndex=0; itemIndex<10; itemIndex ++)
    {
      src >> elements[itemIndex];
      cout << elements[itemIndex] << " ";
    }
    cout << "\n";
    // skip end of line (just in case of comments)
    getline(src, line);
  }
  system("PAUSE");
  return 0;
}

But it's gives me wrong numbers something like this.

Code: Select all

7.3,0.6,10
7.4,0.6,10
E=1 2 11 10 28 29 38 37 2 1
E=2 3 12 11 29 30 39 38 2 1
sobaka
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon Jan 08, 2007 2:43 am

Post by sobaka »

Is it so difficult? :roll:
Post Reply