C # Two ways to implement encoding conversion __ Code

Source: Internet
Author: User

The first way to use C # 's own method encoding:

public string Convencode (string write,string fromencode,string Toencode)

{

Declaring a character set

Encoding from,to;

From = Encoding.GetEncoding (Fromencode);

to = Encoding.GetEncoding (Toencode);

byte[] temp = from.getbytes (write);

byte[] Temp1 = Encoding.convert (from,to,temp);

Returns the converted character

Return to.getstring (TEMP1);

}

The second way to invoke the MultiByteToWideChar method of C + + is

[DllImport ("kernel32.dll")]

public static extern int MultiByteToWideChar (int CodePage, int dwflags,string lpmultibytestr,

int Cchmultibyte, [MarshalAs (UNMANAGEDTYPE.LPWSTR)]string lpwidecharstr, int cchwidechar);

public string Mbytetowchar (String Content,int toencode)

{

Character encoding conversion gb2312:936 utf-8:65001 big5:950 latin1:1252

int len = Pinvoke.multibytetowidechar (toencode,0,content,-1,null,0);

char[] temp = new Char[len];

String content1 = new String (temp);

Pinvoke.multibytetowidechar (Toencode,0,content,-1,content1,len);

return content1;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.