Search code examples
unicodec++-clirichtextbox

C++/CLI richtextbox text to unicode designation (H -> U+0048)


I want to take a character (or space/tab/new line, if possible) from richtextbox1 and convert it into the Unicode designation(correct word?).

Everything I've found thus far tells me about converting the format/encoding. That is NOT my goal. I want richtextbox2 to be the "U+XXXX" equivalent of richtextbox1. I don't have the knowledge/understanding to ask my question more specially. These have been the kind of google searches I've done:

C++/CLI string to Unicode (/w and /wo "designation")

C++/CLI richtextbox->text to Unicode

C++/CLI change text to Unicode-character All of those led to me questions I'm 99% sure I wasn't asking.

This is what I'm trying to get:

private: System::Void RTB1ToUnicodeButton_Click(System::Object^ sender, System::EventArgs^ e) 
{
    if (this->RTB1ToUnicodeButton->Enabled == true)
    {
        richTextBox1->Text = "v"; //as example only, user can input whatever
        String^ WholeText = richTextBox1->Text;
        String^ FirstItem = WholeText->Substring(0, 1);
        FirstItem //something happens here
        richTextBox2->Text = FirstItem // <that = (U+0076)
    }
}

I know I can manually (and tediously) setup a

    if (RTB1->Text == "v")
    { 
        RTB2->Text = "(U+0076)";
    } 
    if (RTB1->Text == "V"
    { 
        RTB2->Text = "(U+0055)";
    } 

However, I'm hopeful there is a much more efficient way. I'll gladly take any input and suggestions.


Solution

  • A IRL friend answered the question. However, I don't know how/why it works.

        System::Text::StringBuilder^ unicodeText = gcnew System::Text::StringBuilder();
    System::String^ text = richTextBox1->Text;
    
    // Loop through each character in the text
    for (int i = 0; i < text->Length; i++)
    {
        wchar_t c = text[i];
        unicodeText->AppendFormat("U+{0:X4} ", (int)c);
    }
    

    I still new and coming back from an 8year break. How/why does "U+{0:X4} " work? I know U+ and the space are in there. but I don't understand why it is now base-16.

    Out of curiosity, could I use this same tool to translate text into base-something-else? If so, what do I put in between the quotes?