Click here to Skip to main content
14,766,371 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hola guys,

I wanna ask something about converting variable.
The story goes when I have :
uString text;

which I need he to be convert as const char *.
I've tried the same way how to convert std::string to const char, but it didn't work.
std::string str;
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0'; 

delete[] writable;
                        // NOOTTTT WORRRKKKK FOR MEE!!!!//////

Anybody knows how to do it?

Updated 29-Oct-12 1:39am
TorstenH. 29-Oct-12 6:40am
I removed the Java Tag - don't know what this has to do with java.
xenotron 29-Oct-12 7:17am
Maybe it uses the ICU library on which java's unicode support builds but ICU has an UString and not an uString. We don't exactly know what is this uString so we can not know how to handle it... I'm afraid the poster has no clear understanding of text encoding.

1 solution

I can't be sure, but it sounds like you have a function that takes a const char* as an input, and all you have is a std::string. If that's the case, you can forget about the const part for now - that's there to indicate to you and be made enforceable by the compiler that no changes will happen to that variable during the function. - This will actively prevent writing data to a var that's a reference.

But, getting back to the issue at hand, how to convert a std::string into something more useful.

How about just this plain and simple 1 liner?

char *writeable = str.c_str();

You can find the docs here:[^]

Note specifically, you (a) don't have to allocate the memory yourself (b) you don't have to free it yourself (c) you shouldn't try to modify it - it's a const char*
satrio_budidharmawan 29-Oct-12 5:18am
Yes, I have function a(int.., const char *, float);

and .., no, I only have "uString" not "String"
enhzflep 29-Oct-12 6:20am
Oh. Hmm - not familiar with that class. It's not part of the stl - there's wString for that. Have you got a url I could follow to look at it?

Also, if it's a wide or unicode string, as I suspect, then you may well _not_ be able to convert it to a meaningful string. In fact, in the unicode 'alphabet' there are far more characters that you couldn't convert than there are that you can convert.
I saw a bunch of uString libs in a google search, but as it's not standard, they're likely to be different, so the exact one is indeed important.
enhzflep 29-Oct-12 14:01pm
Don't forget c/c++ are case sensitive.
So, you'd have a string, not a String. Also, it would be const, not Const.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900