Type conversion

  • Thread starter =?ISO-8859-1?Q?S=F8ren?=
  • Start date
?

=?ISO-8859-1?Q?S=F8ren?=

Hi Guys

I'm still trying to learn C++, and it´s going im the right direction.
The only thing that keeps make me banging my head againts the wall, is
when I have a variable of some sort, and need to use its value in a
different type. :-|

Right now I have two wchar_t* strings (L"Hello World" and L"123"), that
needs to be converted into char* and int.

Could someone please give me a hint on how to do this (need to work with
Mingw in Windows and GCC in Linux), and maybe explain where to find
usefull information for stuff like that, in the future.

Regards Søren Schimkat
 
A

Alf P. Steinbach

* Søren:
Hi Guys

I'm still trying to learn C++, and it´s going im the right direction.
The only thing that keeps make me banging my head againts the wall, is
when I have a variable of some sort, and need to use its value in a
different type. :-|

Right now I have two wchar_t* strings (L"Hello World" and L"123"), that
needs to be converted into char* and int.

Could someone please give me a hint on how to do this (need to work with
Mingw in Windows and GCC in Linux), and maybe explain where to find
usefull information for stuff like that, in the future.

The Boost library.

Note: MingW in Windows, as of version 3.4.4, does not support wide
character streams.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi Guys

I'm still trying to learn C++, and it´s going im the right direction.
The only thing that keeps make me banging my head againts the wall, is
when I have a variable of some sort, and need to use its value in a
different type. :-|

Right now I have two wchar_t* strings (L"Hello World" and L"123"), that
needs to be converted into char* and int.

You really should be using std::wstring unless you can come up with a
convincing reason not to (and you'll have a hard time convincing me).
Could someone please give me a hint on how to do this (need to work with
Mingw in Windows and GCC in Linux), and maybe explain where to find
usefull information for stuff like that, in the future.

Don't know about the first one, what would happen if there's no way to
represent the wchar_t as a char?

The second one on the other hand should be able adapt the solution from
the FAQ
(http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2):


#include <iostream>
#include <string>
#include <sstream>

template<typename T>
inline T wStringToAny(const std::wstring& s)
{
std::wistringstream i(s);
T x;
if (!(i >> x))
throw "Bad conversion";
return x;
}

int main()
{
std::wstring str = L"123";
int i = wStringToAny<int>(str);
std::cout << i << std::endl;
}
 
?

=?ISO-8859-1?Q?S=F8ren?=

Erik Wikström skrev:
You really should be using std::wstring unless you can come up with a
convincing reason not to (and you'll have a hard time convincing me).


Well .. it´s basicly not my choice. The reson for having the conversion
troubles is that I use different librarys, which use the different
types: Irrlicht uses wchar_t* a lot and RakNet seems fond of char* - and
when I have produced a wchar_t string using some Irrlicht function and
needs to present it to RakNet, then the problems arise.
Don't know about the first one, what would happen if there's no way to
represent the wchar_t as a char?


Well .. then I would have to re-think my code, since the wanted
functions return types and input types is the way they are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,530
Members
48,250
Latest member
Bette22B13

Latest Threads

Top