How to Convert String from UTF-8 to Unicode?

The unicode text string shows correctly in the Studio messagebox.
However, the string is being converted to UTF-8 by the Invoke Code activity and unicode character is not showing correctly on the titlebar.

How can I tell the following C# Invoke-Code activity to use UTF-8BOM for titleBarText?

[DllImport(“user32.dll”)]
static extern IntPtr SetWindowText(IntPtr HWND, string Text);
SetWindowText(hwnd, titleBarText);

Hi @grosner,

It’s not the best solution but you can print it in notepad first. Then you can specify encoding while reading from here.

image

Regards,
MY

Thanks but I believe the unicode value is already UTF-8 BOM encoded while it is in UiPath because it renders correctly in the UiPath Messagebox.

Somehow, the encoding is not being picked up in C# and therefore must be encoded before SetWindowText is called.

So I’m still seeking a C# solution.

Got it, you can also try to convert the output like this. Good luck. @grosner

system.Text.Encoding.ASCII.GetString(system.Text.Encoding.UTF8.GetBytes(titleBarText))

@ muhammedyuzuak Not working. Converts:

My Unicode ↑ TitleBar

To

My Unicode ??? TitleBar

Hi,

It seems not UTF-8BOM matter but your application matter (maybe nosupport multibyte characters in title.)

For now, can you try the following sample? It rewrite title of notepad and it works.

Sequence.xaml (7.8 KB)

Regards,

Ok, My application definitely DOES support Unicode characters in the titlebar text.
I did test this with a the Autohotkey application.
What I am seeking is System.Text.Encoding that will change my UTF8 string to Unicode.
The following link shows how to do this in a .Net project (my UiPath project is C# Windows) but I could not get this to work for me in Studio invoke code activity.
UnicodeEncoding encodes Unicode characters using the UTF-16 encoding.

So I am still looking for a way to convert UTF8 to Unicode.

Hi,

Alright. You mean you have UTF8WithBOM data as not byte array but String type, right?
In this case, first we need to convert it byte array properly, because String type is assumed encoding is unicode (UTF-16) and it might be corrupted as UTF8.

For now, can you check it using the following expression, for example?

BitConverter.ToString(System.Text.Encoding.Unicode.GetBytes(yourString))

If it returns proper UTF8 byte array, we can convert it to Unicode.

Regards,

@Yoichi thank you.
Adding CharSet to my DllImport fixed the issue. The unicode characters are now correctly written to the window titlebar text.

Additionall Info:
Specify a character set in C# and C++

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.