juce::String to const char* ?

20 posts / 0 new
Last post
timur
Offline
Last seen: 8 months 2 weeks ago
Joined: 29 Jul 2012 - 11:43
juce::String to const char* ?

Hi all,

Let's suppose that I have created a juce::String from a C-style const char*,

const char* cstring = "Some text";
juce::String string(cstring);

How do I convert it back to a const char*? The standard library has std::string::c_str(), but is there a juce::String function doing the same? I have found juce::String::toWideCharPointer(), which gives me a const wchar_t*, but that's not what I want. I need to pass the string to another function which expects a C-style const char* (not wide character). I figured that I could get a UTF-8 pointer and just cast it to const char*,

const char* new_cstring = static_cast (string.toUTF8());

since ASCII is forward-compatible with UTF-8, but that seems like a very ugly solution and probably wrong. Is there a better way?

Any help appreciated...

jules
Offline
Last seen: 12 hours 52 min ago
Joined: 29 Apr 2013 - 18:37
Re: juce::String to const char* ?

Yes, that's the way. But when you're passing it to a function you don't need to cast it, so it's not ugly at all, e.g.

someOldCFunction (myString.toUTF8());

I think this reads better than c_str because it makes it totally clear that what you're getting is UTF-8, which may be relevant in some situations.

jules
Offline
Last seen: 12 hours 52 min ago
Joined: 29 Apr 2013 - 18:37
Re: juce::String to const char* ?

..and BTW I'd always recommend *never* putting the pointer in a temporary variable like you did, because just like c_str, the pointer will become invalid when the string changes, so there's a danger that if you keep the pointer hanging around, you'll accidentally use it later when it's toast.

timur
Offline
Last seen: 8 months 2 weeks ago
Joined: 29 Jul 2012 - 11:43
Re: juce::String to const char* ?

Yeah, I know it will be invalidated, just like with c_str(), so I am copying it to another char* using strcpy if I want to keep it around.

Thanks for the quick reply!

TheVinn
Offline
Last seen: 2 months 2 weeks ago
Joined: 29 Aug 2009 - 11:31
Re: juce::String to const char* ?

timur wrote:
I am copying it to another char* using strcpy if I want to keep it around.

If I remember how juce::String is implemented, you can probably roll your own simple class that wraps a juce::String and exposes it as a char* with suitable conversion operators. Just keep a reference to the original String when your class constructs and you won't have to make a copy.

Jules is this possible?

jules
Offline
Last seen: 12 hours 52 min ago
Joined: 29 Apr 2013 - 18:37
Re: juce::String to const char* ?

Sure, it's possible to use strcpy or to build your own wrapper object. But the best way to keep a copy of that char* is to just keep the String object, and then call toUTF8() on it whenever you need the raw pointer.

I wouldn't use strcpy, because it's just so 1990s. And it's far less efficient than taking a copy of the String, which would simply involve a ref-count, not a malloc/copy.

And I wouldn't wrap it in an object with an implicit cast to const char*, because this isn't an operation that should be hidden from view. I deliberately avoided giving the String class an operator const char*() because I think it's best to always see "toUTF8()" in code that uses it, so that any reader will immediately be aware that a raw pointer is being taken, and that the encoding is UTF-8.

And it's worth noting that toUTF8() is completely free - it just returns a pointer to the underlying string, so compilers will optimise it down to nothing. So there's no reason at all to bother caching the pointer that it returns.

Wolfen
Offline
Last seen: 2 days 22 hours ago
Joined: 13 Jan 2005 - 18:05
Re: juce::String to const char* ?

For information, I'm wondering if the two implementations discussed here are really equivalent... I had a hard time creating a function which converts ANSI coded text files into Unicode. Because the text files contain french characters, I was unable to succeed using any standard JUCE::String function, because of the juce_wchar behaviour.

For example, I have somewhere the character "é", and I wanted to detect it to convert it into CharPointer_UTF8 ("\xc3\xa9"). The only way to get the value '-23' or the unsigned equivalent was to use "static cast ". Any solution based on juce_wchar gives me '9' instead... Maybe I have done something wrong somewhere, but finally I was able to make it work. Moreover, the text files I am talking about were written with JUCE 1.49 if I remember correctly :mrgreen:

Working with Windows French ANSI text files is really a pain...

TheVinn
Offline
Last seen: 2 months 2 weeks ago
Joined: 29 Aug 2009 - 11:31
Re: juce::String to const char* ?

"é" is this standard ASCII? I don't think so... you would need a special conversion table, and treat the JUCE string as plain ASCII with no conversion.

I could be wrong though.

Wolfen
Offline
Last seen: 2 days 22 hours ago
Joined: 13 Jan 2005 - 18:05
Re: juce::String to const char* ?

I'm not an expert about ASCII codes, but the ASCII tables I have seen had all the time the same "extended characters" :

http://www.asciitable.com/

Anyway, I was talking about that because the solution I have found to replace the characters looks not optimal for me, and because I think a "standard" JUCE function should return ASCII codes between 0 and 255 instead of 0 and 127... It's just my opinion :mrgreen:

ckk
Offline
Last seen: 19 hours 27 min ago
Joined: 20 May 2010 - 19:43
Re: juce::String to const char* ?

ASCII is only the the characters from 0 to 127, see here for example.
AFAIK the extended ASCII may vary depending on the system language and such.

Chris

jrlanglois
Offline
Last seen: 4 hours 34 min ago
Joined: 12 Aug 2011 - 17:54
Re: juce::String to const char* ?

Quote:
and because I think a "standard" JUCE function should return ASCII codes between 0 and 255 instead of 0 and 127... It's just my opinion :mrgreen:

:shock: You do know that you can't get a value beyond 127 with a signed char on a platform where sizeof(char) = 1 byte, right?

As TheVinn said exactly;

Quote:
you would need a special conversion table, and treat the JUCE string as plain ASCII with no conversion.

[Visual Studio 2013] [Xcode 5.1.1]
[latest JUCE tip] [CodeTools]

Pages