Simple conversion

Hi!

I had to convert a string which contains a double value followed by other information. I know the starting point of the value within the string but not the length/end of the number.

The VC++ Express 2005 documentation tells me that System::Convert::ToDouble would be the equivalent to wcstod. But that's not true. wcstod gives me the last sucessfully converted string character, ToDouble doesn't do this.

I had a long search in the library documentation but I didn't find any .NET function which is able to convert/parse a string to a double number AND gives me additional information about where the number-conversion stopped within the string.

So, does anybody know a full wcstod replacement accepting String objects immediately

Thanks in advance!


Answer this question

Simple conversion

  • OHDev

    is the following acceptable

    wchar_t *stopstring;
    String^ text(
    "234t67");
    pin_ptr<const wchar_t> wch = PtrToStringChars(text);
    double d1=wcstod(wch,&stopstring );


  • sieler

    I don't know of a similar function for StringBuilder directly (without using ToString()).

    As for copying String to a StringBuilder rather than creating new everytime, you can use Clear() and Append() in order to use the sb object you already have.

       StringBuilder^ sb = gcnew StringBuilder("to be overwritten");
       String^ s = gcnew String("to be copied");

       sb->Remove(0,sb->Length);
       sb->Append(s);

    In addition to the MSDN help on String and StringBuilder, you may want to take a look at blogs such as :
    http://blogs.msdn.com/msdnstudentflash/archive/2005/01/31/364217.aspx
    http://blogs.wwwcoder.com/shaunakp/articles/2157.aspx

  • Scott21

    A good workaround. I will use this. I already tried to use a pin_ptr workaround but I didn't notice the PtrToStringChars function. Many thanks for your help!

    Do you know if a simliar function (like PtrToStringChars) exists for StringBuilder I changed the source object type to StringBuilder so I would have to call ToString() before passing it to PtrToStringChars().


    I think it will be a long, long way until I can use C++/CLI/.NET as easy and naturally as conventional C++. Sigh. There are so many very simple problems when porting old programs. I am not able to solve many of them elegantly.

    e.g. this one:

    I tried to simply copy the content of a String object to a StringBuilder object. I was able to do this only by creating a new StringBuilder instance:

       StringBuilder^ sb = gcnew StringBuilder("to be overwritten");
       String^ s = gcnew String("to be copied");

       sb = gcnew StringBuilder(s); // how to copy instead of making a new instance

    I had a look an all methods of String and StringBuilder and didn't find an other way. Why don't the libraries simply provide an operator for this purpose This would be so very easy to use.


  • Simple conversion