Search code examples
firemonkeyc++builderc++builder-10.3-rio

Add angle symbol to string


How can i add an angle symbol to a string to put in a TMemo?

I can add a degree symbol easy enough based on its octal value from the extended ascii table:

String deg = "\272";  // 272 is octal value in ascii code table for degree symbol
Form1->Memo1->Lines->Add("My angle = 90" + deg);

But, if i try to use the escape sequence for the angle symbol (\u2220) i get a compiler error, W8114 Character represented by universal-character-name \u2220 cannot be represented in the current ansi locale:

UnicodeString deg = "\u2220";
Form1->Memo1->Lines->Add("My angle = 90" + deg);

Just for clarity, below is the symbol i'm after. I can just use the @ if i have too, just wondering if this is possible without nashing of teeth. My target for this test was Win32 but i'll want it to work on iOS and Android too.

enter image description here

p.s. This table is handy to see the codes.

After following Rob's answer i've got it working but on iOS the angle is offset down below the horizontal with the other text. On Win32 it is tiny. Looks good on Android. I'll report as a bug to Embarcadero, albeit minor.

results

android result

Here is code i used based on Rob's comments:

UnicodeString szDeg;
UnicodeString szAng;
szAng.SetLength(1);
szDeg.SetLength(1);
*(szAng.c_str()) = 0x2220;
*(szDeg.c_str()) = 0x00BA;
Form1->Memo1->Lines->Add("1: " + FormatFloat("##,###0.0",myPhasors.M1)+ szAng + FormatFloat("###0.0",myPhasors.A1) + szDeg);

Here is how looks when explicitly set the TMemo font to Courier New:

after setting font to Courier New

Here is the final code i'm using after Remy's replies:

UnicodeString szAng = _D("\u2220");         
UnicodeString szDeg = _D("\u00BA");
Form1->Memo1->Lines->Add("1: " + FormatFloat("##,###0.0",myPhasors.M1)+ szAng + FormatFloat("###0.0",myPhasors.A1) + szDeg);

Solution

  • The compiler error is because you are using a narrow ANSI string literal, and \u2220 does not fit in a char. Use a Unicode string literal instead:

    UnicodeString deg = _D("\u2220");
    

    The RTL's _D() macro prefixes the literal with either the L or u prefix depending on whether UnicodeString uses wchar_t (Windows only) or char16_t (other platforms) for its character data.