1

Greedings,

I've been looking through everything with google, but i just cannot find an answer.

How can i convert a string "5" to an integer 5 in Universal Windows Platform (UWP) with C++ ?

I've already tried to just convert it by (String^) and so, i know it's pointless but you never know with UWP.

The msdn documentation does not describe anything about type conversions and i just can not find it anywhere. I do not want to do something like String ^ => wchar_t => char -> atoi. Is there a better method for this ? or do i have to do this long memorytaking process ?

EDIT: It's not the same as you marked... can you read my description before you mark it ? The link you've send is converting std::string to an integer, that's easy, but i need to know how to convert String^ To an int (int32)

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Lukasas
  • 139
  • 8
  • 1
    @Sam: I don't think he's asking about converting string→`int` in general, but rather the special case UWP `String^`→`int`. And that's not covered by the potential duplicate (namely http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c). – Cheers and hth. - Alf Oct 23 '16 at 23:03
  • I suspect the `Platform::String::Data` member function will let you use the ordinary string to int methods such as `std::stoi`. See (http://stackoverflow.com/questions/11746146/how-to-convert-platformstring-to-char). – Cheers and hth. - Alf Oct 23 '16 at 23:07
  • Well, i've ended up with wcstol, but i was thinking about something like ToInt like in C#... something that doesn't use ...tol(str, wcs..) functions – Lukasas Oct 23 '16 at 23:14
  • There's `System::Int32::Parse`. I've not even looked at UWP so I don't know if it's available, but if it is then that's fairly direct. – Cheers and hth. - Alf Oct 23 '16 at 23:37
  • @Lukasas Have you looked into using C++'s stringstream? – MrEricSir Oct 23 '16 at 23:38
  • @Cheersandhth.-Alf: `System::Int32::Parse` is .NET (i.e. the managed runtime and platform). UWP, on the other hand, is a native, unmanaged implementation. While it's possible to use a subset of .NET (namely .NET Core), it would be overkill to drag in the entire .NET Core dependency just for this trivial task. – IInspectable Oct 24 '16 at 00:41
  • @IInspectable: Thanks. I probably need to do some UWP exploration just in order to not sound like a novice. But, I don't really see any future for the idea of single-code-for-all-platforms, it sounds just like Java, maybe with same prime lifetime (10 years?), so I'm pretty hesitant to use time on it. – Cheers and hth. - Alf Oct 24 '16 at 01:24
  • @Cheersandhth.-Alf: If you do want to explore the platform, make sure to look into [C++/WinRT](https://github.com/Microsoft/cppwinrt), a standard C++ language projection for the Windows Runtime. It's still a fair bit unpolished, lacks support for certain facilities (like XAML), and documentation is scarce. I'd still consider it the most direct way to learn about the platform, without getting distracted by the tooling (like you would with C++/CX or C#). – IInspectable Oct 24 '16 at 01:33

1 Answers1

4

A Platform::String (represented as String^ in C++/CX) provides the String::Data member, that returns a const char16* to the internal buffer. It can then be used with any standard C or C++ string conversion functions, like std::wcstol:

long ToLong( String^ str ) {
    const wchar_t* begin = str->Data();
    return std::wcstol( begin, nullptr, 10 );
}

Alternatively, if you want to implement some error handling, and ensure that the entire string is interpreted, you could write:

long ToLong( String^ str ) {
    const wchar_t* begin = str->Data();
    const wchar_t* end = str->Data() + std::wcslen( str->Data() );
    wchar_t* last_interpreted{ nullptr };
    long l = std::wcstol( begin, &last_interpreted, 10 );
    if ( last_interpreted != end ) {
        throw ref new InvalidArgumentException();
    }
    return l;
}

Note, that no additional memory is allocated. The conversion function operates on the stored sequence of the Platform::String.

If you can spare a potential temporary memory allocation, you could use std::stol, and get proper error reporting for free:

long ToLong( String^ str ) {
    return std::stol( { str->Data(), str->Length() } );
}
IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) provides error checking and is very simple to use. :) – Cheers and hth. - Alf Oct 24 '16 at 01:27
  • @Cheersandhth.-Alf: It is, indeed. But it is at the mercy of the optimizer, to produce an equally zero-overhead conversion. The `const std::wstring` may (or may not) have to allocate an extra copy. Whatever, I'll just add it for completeness. – IInspectable Oct 24 '16 at 01:36