We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

wstring conversion

Hi!

I'm trying to read wstring from file.

This is an file content example:
"83
118
269
...
"
The number 83 represent "S", 118 represent "v", 269 represent "č", and so on..

So, I tried to read the numbers into int. But the problem is, that I don't know how to convert int to wstring, to get actual letter(convert: int value of 83 to letter "S").

tnx

3
Contributors
6
Replies
4 Hours
Discussion Span
7 Months Ago
Last Updated
7
Views
mikesr
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

If you perform the command
char c=83
you will get that c is equal to 'S'

edit: sorry didnt see the wstring :O only works on char

Despairy
Junior Poster
123 posts since Mar 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

You will find the information in any ascii chart, such as this one. Since C/C++ and other languages represent letters are numbers there is no conversion from one to the other except for typecasting. If you want to display the letter 'S' all you do is typecast it to char, for example

int letter = 83;
cout << (char)letter;
Ancient Dragon
Achieved Level 70
Team Colleague
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68

Ancient Dragon - is there a difference between the wstring and the char?
I saw somewhere that by using the boost library you can transform the number into its wstring
and if im not mistaken its more characters than the ascii table can provide ( like his 269 one )

thanks

Despairy
Junior Poster
123 posts since Mar 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

yes I also need the c-caron, s-caron, ... letters. for example, number 269 will not work on char

mikesr
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

you could copy the ints into wchar_t variables or std::wstring. What characters are displayed on the screen will depend on what language(s) are installed in the computer. For MS-Windows also look into setlocale()

Ancient Dragon - is there a difference between the wstring and the char?

Yes, wchar_t is 2 or more bytes per character while char is only 1 byte per character. std::wstring is an array of wchar_t characters. std::wstring and std::string are not easily convertable from one to the other. Furthermore, the size of wchar_t is not constant from one os to another. On MS-Windows sizeof(wchar_t) is 2 while on *nix it is 4. I read a couple years ago that UNICODE standards committee is considering expanding it to 8 so that it can hold graphics characters used in some languages.

Ancient Dragon
Achieved Level 70
Team Colleague
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68

It works! tnx

solution:

wstring letters =L"";
int number=269;
letters += (wchar_t)number;
wcout<<letters;
mikesr
Newbie Poster
17 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3049 seconds using 2.7MB