# New String problem with high-ascii values

**URL:** <https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317>\
**Category:** General JUCE discussion\
**Created:** [January 30, 2011, 2:51am UTC](https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317 "2011-01-30T02:51:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 30, 2011, 2:51am UTC](https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317/1 "2011-01-30T02:51:15Z")

</div>

This produces the wrong result:

```auto
String s("÷2");
```

I believe this is the problem:

juce\_String.cpp:

```auto
String::String (const char* const t)
    : text (StringHolder::createFromCharPointer (CharPointer_UTF8 (t)))
{
}
```

const char\* const t is not really a utf-8 encoded string, its an ASCII string.

I think the solution is to just introduce CharPointer\_ASCII, with its obvious implementation: each ASCII character maps 1:1 to a UTF-32 code point of the same value, and change the constructor for String(const char\* const t).

I’m not 100% sure about all of this though, so don’t hesitate to correct me.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 30, 2011, 12:39pm UTC](https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317/2 "2011-01-30T12:39:30Z")

</div>

> [@](#):
>
> const char\* const t is not really a utf-8 encoded string, its an ASCII string.

It’s only in ascii because your source-code editor saved the file as ascii. If it had saved it as utf-8, it’d work just fine.

Sadly, there’s only one portable way to encode strings with characters above 0x7f, and that’s by using escaped utf-8 character codes inside char\* literals. There’s no other way to reliably get your string from the editor into the compiler and then into the code without risking the encoding being lost somewhere along the way. That’s why I made the String class assume it’s getting utf-8 - the alternative would be to assume that it’s ascii or a local encoding, and they’re worse options.

I’ve already been thinking about adding a CharPointer\_ASCII class, and my cunning plan is to make the String (const char\*) constructor slightly special - it’d assume the string it’s getting is unambiguously ascii, so that if you tried to feed it a value above 0x7f, it’d throw an assertion. That would force you to use a different constructor for extended strings, so in your case you’d have to write String (CharPointer\_ASCII (“÷2”)). That would mean that it’s the coder’s responsibility to explicitly wrap these strings in an encoding that matches their source-file format.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 30, 2011, 12:46pm UTC](https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317/3 "2011-01-30T12:46:45Z")

</div>

I tried changing the source file with the high-ascii constant to save as “Unicode (UTF-8 with signature) - Codepage 65001” and it didn’t help…

Not sure what to do here.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [January 30, 2011, 12:54pm UTC](https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317/4 "2011-01-30T12:54:36Z")

</div>

Be careful adding the BOM to source files, as apparently gcc doesn’t handle it correctly.

The only “correct” thing to do would be to escape it as utf-8, which will at least guarantee it’ll work everywhere.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [January 30, 2011, 1:04pm UTC](https://forum.juce.com/t/new-string-problem-with-high-ascii-values/6317/5 "2011-01-30T13:04:05Z")

</div>

This worked:

```auto
b->getFacade().setTextLabel (TRANS("\xC3\xB7" "2"));
```
