unicode string

Started by
4 comments, last by SiCrane 16 years, 3 months ago
I get an error when using a filename with unicode The strings must have an L in front but this fails std::string a; a="L"+fileName.c_str(); //error cant add 2 pointers together hr = D3DXCreateTextureFromFile( l_device,a, &l_tex);
Advertisement
Well, the problem is just as it says; you can't add two strings!

The term "String" is a bit vague. A string, used in C/C++ and not regarding the stl almost always refers to an array of bytes, each letter of the string being a byte (and for "null terminated strings, ending with a byte with a vlue of 0 to signify the end of the string). As is such, an array is just a block of memory, pointed to by, of all things, a pointer. This is why you will see strings of text sometimes declared as:

(const) (unsigned)char*
(const) (unsigned) char*

With the words within brackets being optional.

C++ doesn't define an operator for adding two of these strings together, because if it did it may lead to some strange behavior if someone was to use one of the above variable types for a different purpose.

This is where the std::library comes in. the std::string encapsulates the above into something thats easier to work with without worrying about pointers. As such, it defines the operators + and += operators for not only adding std::string's together, but std::string's to the above types.

so, you could write:

std::string a;

a += "L";
a += fileName;

But, because the operators are defined for forward and reverse cases (adding an std::string to something else, AND adding an something else to an std::string), you can further simplify the above by writing:

a = "L" + fileName;

There are other ways to do this (check out string streams), but one of those two should work for you.

PS: By the way, joining two strings together is referred to as "concatenating", and you might find some other code or library bit out there that might help.

[Edited by - Liam M on January 9, 2008 1:59:01 AM]
The L is not part of the string. It should be put in front of it like so:
func(L"hello");


However, this only works with string literals (things that have " on both sides).

As far as I know, std::wstring (notice the 'w') handles unicode strings so try this:
std::wstring a("file.bmp");hr = D3DXCreateTextureFromFile( l_device, a.c_str(), &l_tex);
Quote:Original post by Gage64
The L is not part of the string. It should be put in front of it like so:
func(L"hello");


However, this only works with string literals (things that have " on both sides).

As far as I know, std::wstring (notice the 'w') handles unicode strings so try this:
std::wstring a("file.bmp");hr = D3DXCreateTextureFromFile( l_device, a.c_str(), &l_tex);


Woops, just noticed the "unicode" in the title. What encoding is a std::wstring?
I don't use DirectX, so I'm not familiar with the naming conventions, but apparently the non-unicode version is "D3DXCreateTextureFromFileA" as opposed to "D3DXCreateTextureFromFile".

Quote:Original post by Liam M
Woops, just noticed the "unicode" in the title. What encoding is a std::wstring?

Implementation defined. It may not even be Unicode, though most implementations use UCS-2.

This topic is closed to new replies.

Advertisement