473,473 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Type conversion

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
Apr 30 '07 #1
3 1676
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 30 '07 #2
On 2007-04-30 21:09, Søren wrote:
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-lit...tml#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;
}

--
Erik Wikström
Apr 30 '07 #3
Erik Wikström skrev:
On 2007-04-30 21:09, Søren wrote:
>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).

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.
>
>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?

Well .. then I would have to re-think my code, since the wanted
functions return types and input types is the way they are.
>
The second one on the other hand should be able adapt the solution from
the FAQ
(http://www.parashift.com/c++-faq-lit...tml#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;
}
May 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.