String to char*


#1

Hey

How would I convert a String into either a std::String or char*? The reason being I have another library that cannot understand Juce Strings.

Thanks.


#2

…just cast it to a const char*. There are plenty of comments about this in the docs.


#3

const char* cstr = (const char*)JUCEString;


#4

Aye.

That’n’all.


#5

Oh, its that simple…
I have allways done JUCEString->toUTF8() :slight_smile:

EDIT: But when thinking about it, “->toUTF8()” is actually shorter than “(const char*)”…


#6

Oh, its that simple…
I have allways done JUCEString->toUTF8() :slight_smile:

EDIT: But when thinking about it, “->toUTF8()” is actually shorter than “(const char*)”…[/quote]

Yeah, but I don’t know what the fuck UTF8 is!


#7

Oh, its that simple…
I have allways done JUCEString->toUTF8() :slight_smile:

EDIT: But when thinking about it, “->toUTF8()” is actually shorter than “(const char*)”…[/quote]

Yeah, but I don’t know what the fuck UTF8 is![/quote]

Ignorant idiot.


#8

that’s a bit harsh!


#9

Sorry.


#10

I just said to Jade just now, “What the fucks UTF8?”, she didn’t know either!

I propose we change it’s name to WTF8, so as to clear up any confusion.


#11

I just said to Jade just now, “What the fucks UTF8?”, she didn’t know either!

I propose we change it’s name to WTF8, so as to clear up any confusion.[/quote]

Excellent idea!


#12

c’mon, guys ! print this article to read it next time you wait for your application to compile and link.


#13

Jules mate, I think our sense of humour is wasted on these people.


#14

Didn’t mean to be too serious, just my 2 cents…
actualy I like the wtf idea a lot, I think juce would benefit from being less clean and well written, to give it some more personnality.
grep -r ‘wtf’ juce/src/* gives nothing and that’s too bad…


#15

[quote=“thomas”]Didn’t mean to be too serious, just my 2 cents…
actualy I like the wtf idea a lot, I think juce would benefit from being less clean and well written, to give it some more personnality.
grep -r ‘wtf’ juce/src/* gives nothing and that’s too bad…[/quote]

lol. aye! we could have the juce equivalent of Javas “Object” or COMs “IUnknown” (I think) where all Juce classes are descendants of class JuceWTF!

well it tickled me.


#16

Is this const char* cast still supposed to be working? It gives me a compiler error.
Also, I’m a little confused about the documentation of toUtf8(). In the doc, it says, it’ll return a reference to the string’s internal data. However, when stepping through the code, it creates a copy of the internal data, and then the copy gets lost when the function returns.


#17

Casting a string to a const char* would be meaningless because it wouldn’t specify what format the 8-bit data should take - that’s why you should call toUTF8 or toCString instead.

Don’t worry about how it works, the only thing you need to be aware of is that the pointer may become invalid when you call another method on the string object.


#18

yea, that’s why I was puzzled that so many people (including you) said it would work.

my problem is that I’m trying to return a pointer of what toUtf8() returned from an AudioPluginInstance object.
So, my code looks something like this

const char *MyClass::GetPluginName()
{
return audioPluginInstanceObject->getName().toUtf8();
}

this doesn’t work for some reason (the returned pointer shows undefined data). I tried using toCString but got the same result.


#19

Well of course that won’t work! :roll:

When I said “the pointer that is returned becomes invalid as soon as you call another method on the String object”, that obviously also includes calling its destructor!


#20

right…
can you think of solution to my problem? I basically just want to get a reference to the String’s internal data, that I can pass through functions as a const char *, kind of the way string::c_str() works.