String representation

I thought exactly as you did initially.

I have read all this several times and I am getting the idea but to be sure please tell me how to do the following.

Display all the characters in a font from 32 to 255 in UTF_8, one character at a time.

for ( int i = 32; i < 255; i++ ) { display the font character i as a single character }

I would think you cast the loop variable to a utf32 char and then turn that into a UTF-8 String using a constructor or free/static function?

Can you be more specific? I am still new to juce.

Not sure what you mean about a font, but this would get you the raw utf8 sequence for each of those unicode characters:

[code]for (int i = 32; i < 255; ++i)
{
String s (String::charToString (i));
char* rawUTF8sequence = s.toUTF8();

}
[/code]

If you’re drawing text using a font, you don’t need to worry about utf-8, just create the string from that unicode char, and draw it.

That is what tried but the string does not display characters 128-160. Any ideas?

What do you mean by “display”?? Print them to the console, or draw them to the screen? If so, how? Show some code.

For this you need to use a Bloom filter to start, and then get fancy with some more data structures. But we’re still back to the same bottleneck - memory access is hideously slow, and becomes slower (relatively speaking) as you increase the number of cores or the clock speed of each core. So the less memory you touch, the faster you go. Smaller strings = more speed.

Here is a quick example that does not draw the characters from 128-160
I haven’t checked it out with all types of fonts but symbol fonts and music fonts are not ascii fonts so some of them use characters from 128 to 255

for ( int i = 0; i < 32; i++ ) { String s( String::charToString( i + 128) ); g.drawSingleLineText( s, 50 + (i*20), 170 ); }

It will draw all others from 32-255

Well I can’t think of any reason why that wouldn’t work… Assuming that you’ve selected a font that really does have those characters in it, then it should be fine. The string representation is definitely not the problem, and we know that the font system can show extended characters… Try tracing into it and see what happens?

Sorry but I have read and read and still don’t know the correct simple cross platform way of doing this:

User enters “Mäßig” into an edit field. I want to save this in a fixed array of characters such as char saveChars[256] and then later convert the array to a string to be displayed. I do not want to store this in a Juce String and I am not concerned with the size of the array needed.

Please someone give me a simple straight example of how to do this.

toUTF8() and fromUTF8()

That’s the only two methods you need.

If you don’t intend to modify the text, then there’s no reason whatsoever to copy it into an array - just leave it in a String and use toUTF8() to access the bytes directly if you need to.

If you do intend to modify the text, then you should never do that in a fixed-size array, you should use a String, std::string, Array, etc etc instead.

Fixed-size arrays are BAD PRACTICE in modern C++! Don’t do it, kids!

looking back, JUCE strings have definitely come a long way!

Thanks and I got it to work. I had no intention of modifying it while in the array, just storing and retrieving the correct Juce way.

As for the advice on fixed sized arrays, not needed, and not correct. Fixed size arrays are only dangerous in the hands of those who don’t know what they are doing.

Also, I assume there is still no way of copying text like this, directly into source code. Does it have to be converted to an escape sequence?

And while we all don’t know what we’re doing from time to time, fixed size arrays might be dangerous to all of us.

Well, I don’t know whether it was needed or not, but it’s definitely correct! Ask any C++ guru nowadays, they’ll say exactly what I said: use fixed size arrays only when there’s no alternative.

Sure, they can be used safely. So can guns and explosives. And obviously for some kinds of low-level performance-critical hackery, they’re the right tool for the job. But for mundane things like storing a string? Definitely not!

The introjucer has a tool that lets you paste text and will convert it to a valid C++ literal.

I love you guys. I used the word ‘array’ to see how you all would responded and got what I expected. I am actually storing them in a memory buffer, not a array of characters that I index or use as a array.

Just having fun, KIDS.

No need to take offense!

I’ve no idea whether you’re a battle-hardened veteran C++ guru, or a struggling n00b…! But we do get a lot of n00bs on here, so am just trying to spread a bit of fatherly advice!

wow. My brain hurts. Thanks prof Vinn