unicode stringstream

Hi,

I used to do this while I used ASCII characters, but now I need to use UNICODE, and I couldn't find the answer after googling some, and searching the forums.

1
2
3
4
std::stringstream ss;
int some_int = 45;
ss << "Some text = " << some_int << "\n";
do_something_with_the_text ( ss.str() ); //takes std::string 


This worked fine in ASCII but when I try the same in Unicode:

1
2
3
4
std::wstringstream ws;
int some_int = 45;
ws << L"Some text = " << some_int << L"\n";
w_do_something_with_the_text ( ws.str() ); //takes std::wstring 


It doesn't work.

How can I do this in Unicode?

Best regards,
Yours3!f
Last edited on
What doesn't work?
closed account (DSLq5Di1)
I don't see any reason for that not to work, could you post more details.. where is the problem exactly? what happens in w_do_something_with_the_text?
ok, so w_do_something_with_the_text takes a wstring to render a unicode string onto the screen.
1
2
3
4
5
6
//something like this
for(int c = 0; c < str.size(); c++)
{
  int character_code = str[c];
  render(characters[character_code]);
}

With ASCII it works with unicode nothing shows up on screen, which indicates that there's no such character in the font. With wstring the rendering works fine. To add I can print the wstringstream with wcout. Is there a conversion issue between wstringstream and wstring?
Last edited on
Does L"Some text = " display OK?

What system are you using? And toolset?

And what kind of characters are you talking about?
no it doesn't, same thing happens
I use kubuntu 11.04 64bit, the IDE is kdevelop 4 with latest compiler
a font consists of a character map which contains characters that you can access using their character code. I think that because ws.str() returns a basic_stringstream<wchar_t> the character codes get messed up. so is there a way to convert a wstringstream to a wstring preserving the character codes, or adding non-string stuff as characters to strings? like I added some_int to ws
ok, I found this, but as the article says this doesn't work for linux...
http://cfc.kizzx2.com/index.php/tag/wstringstream/
is there a linux way of doing this?
The code seems OK (about the wstringstream). You are probably getting an OK wstring. Most likely the problem is with render or the font selected. Since I don't know linux, I cannot help any further. I recommend that you post in the Linux forum (or transfer this post there).
ok, I transferred... I think that the rendering code is ok, because if i use plain wstring I can render all the characters on my keyboard, which indicates that the font does contain those characters (so it should be ok too)
ok, I wrote a small example which writes out wstrings and wstringstreams and their character codes. Since all codes are the same and in the previously mentioned rendering function I get the same codes I guess the rendering function is wrong...

the example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <sstream>

void write_str( std::wstring wstr )
{
    for (int c = 0; c < wstr.size(); c++)
    {
        int code = wstr[c];
        std::wcout << code << L" ";
    }
    std::wcout << std::endl;
}

int main(int argc, char **argv)
{
    std::wstringstream ws;
    std::wstring wstr;
    int num = 12;
    
    ws << num << L" hello";
    wstr = ws.str();
    
    std::wcout << ws.str() << std::endl;
    write_str(ws.str());
    
    std::wcout << ws.str().c_str() << std::endl;
    write_str(ws.str().c_str());
    
    std::wcout << wstr << std::endl;
    write_str(wstr.c_str());
    write_str(wstr);
    
    wstr = ws.str().c_str();
    std::wcout << wstr << std::endl;
    write_str(wstr);
    write_str(wstr.c_str());
    
    std::wstring wwstr = L"hello";
    std::wcout << wwstr << std::endl;
    write_str(wwstr);
    
    return 0;
}



12 hello
49 50 32 104 101 108 108 111 
12 hello
49 50 32 104 101 108 108 111 
12 hello
49 50 32 104 101 108 108 111 
49 50 32 104 101 108 108 111 
12 hello
49 50 32 104 101 108 108 111 
49 50 32 104 101 108 108 111 
hello
104 101 108 108 111 
Last edited on
I finally solved it, can't believe how stupid the mistake was... I swapped buffers before drawing the text, and since in the beginning of the next frame I clear the back buffer the text gets cleared too... this was an epic 2 days of searching where I messed up... Well thanks for all the help guys!
Topic archived. No new replies allowed.