How to convert UNICODE strings to MBCS in c#?

4.8k Views Asked by At

I have a UNICODE string (chinese) which I want to convert back to MBCS so as to add it as a parameter to an SQL query. (the column in SQL Server in varchar and so this conversion is necessary for me).

How can I convert to MBCS in c#? Please help.

Thanks, Praseo

3

There are 3 best solutions below

1
On

“MBCS” could be a number of encodings. For the China locale, it would be code page 936, a GB-variant, and you could encode to its bytes using:

byte[] bytes= Encoding.GetEncoding(936).GetBytes("你好")
=>
{196, 227, 186, 195}

If you aren't specifically talking about GB encoding and want whatever multibyte encoding is the default for your current system, then you can just use the Default (“ANSI”) encoding which will be that MBCS encoding:

byte[] bytes= Encoding.Default.GetBytes("你好")
=>
{196, 227, 186, 195}  // on a system in the China locale (cp936)
{167, 65, 166, 110}   // on a system in the HK locale (cp950)

Now to get your byte array into the query you'll have to either:

1) best, use a parameter with a byte-based SqlDbType, eg:

command.Parameters.AddWithValue("@greeting", bytes);

2) orif you have to insert it directly into the query string, encode the bytes as a hex literal, eg:

"0x"+BitConverter.ToString(bytes).Replace("-", "")
=>
... WHERE greeting=0xC4E3BAC3 ...

(this is non-ANSI-standard SQL Server syntax)

0
On

Thanks bobince for the hint. I could not parameterize my query execution though.

Finally, one of my senior colleagues helped. :) Here is another solution to convert a Unicode encoded string to one consisting of Multibyte characters(MBCS) for comparison in T-SQL against varchar columns in legacy database.

We can make use of the method GetNonUnicodeBytes() of the class SqlString.

Here is a code snippet below.

        string inputUnicodeString = "你好";
        SqlString mySqlString = new SqlString(inputUnicodeString);
        byte[] mbcsBytes = mySqlString.GetNonUnicodeBytes();
        string outputMbcsString = string.Empty;
        for (int index = 0; index < mbcsBytes.Length; index++)
        {
            outputMbcsString += Convert.ToChar(mbcsBytes[index]);
        }

This helped me compare the required varchar database column with user requested string in UNICODE. Hope this reply helps others.

Regards,

Praseo

1
On

I'm not sure whether you want this:

string result = Encoding.GetEncoding("gb2312").GetString(Encoding.Unicode.GetBytes(yourStr));