Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert unicode to utf-8 encoding in c#

Tags:

c#

unicode

utf-8

I want to convert unicode string to UTF8 string. I want to use this UTF8 string in SMS API to send unicode SMS. I want conversion like this tool https://cafewebmaster.com/online_tools/utf8_encode

eg. I have unicode string "हैलो फ़्रेंड्स" and it should be converted into "हà¥à¤²à¥ à¥à¥à¤°à¥à¤à¤¡à¥à¤¸"

I have tried this but not getting expected output

    private string UnicodeToUTF8(string strFrom)
        {
           byte[] bytes = Encoding.Default.GetBytes(strFrom);

           return Encoding.UTF8.GetString(bytes);

        }

and calling function like this

string myUTF8String = UnicodeToUTF8("हैलो फ़्रेंड्स");
like image 823
satyender Avatar asked Jan 20 '26 10:01

satyender


1 Answers

I don't think this is possible to answer concretely without knowing more about the SMS API you want to use. The string type in C# is UTF-16. If you want a different encoding, it's given to you as a byte[] (because a string is UTF-16, always).

You could 'cast' that into a string by doing something like this:

static string UnicodeToUTF8(string from) {
    var bytes = Encoding.UTF8.GetBytes(from);
    return new string(bytes.Select(b => (char)b).ToArray());
}

As far as I can tell this yields the same output as the website you linked. However, without knowing what API you're handing this string off to, I can't guarantee that this will ultimately work.

The point of string is that we don't need to worry about its underlying encoding, but this casting operation is kind of a giant hack and makes no guarantees that string represents a well-formed string anymore.

If something expects a UTF-8 encoding, it should accept a byte[], not a string.

like image 65
Kyle Avatar answered Jan 22 '26 23:01

Kyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!