locked
Chinese character conversion RRS feed

  • Question

  • Hi Guys,

    I have broken(junk) Chinese data in my source table and wanted to take them back to actual(Chinese) type of data. can you please provide the sample code to convert String data into Chinese data.

    Thanks,

    Khaleel

    Wednesday, September 7, 2016 3:35 PM

Answers

  • Hi Khaleel123,

    Thanks for posting there.

    >>can you please provide the sample code to convert String data into Chinese data.

    Can you please tell me which way is your Chinese Data coding ?(Unicode or utf-16)

    If you use Unicode ,you can use Stream Reader class which implements a TextReader that reads characters from a byte stream in a particular encoding . You can use StreamReader Constructor (Stream, Encoding),Here is a code sample :

    private void getNewStreamReader() { //Get a new StreamReader in ASCII format from a //file using a buffer and byte order mark detection StreamReader srAsciiFromFileFalse512 = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ASCII format from a //file with byte order mark detection = false StreamReader srAsciiFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII, false); //Get a new StreamReader in ASCII format from a file StreamReader srAsciiFromFile = new StreamReader("C:\\Temp\\Test.txt", System.Text.Encoding.ASCII); //Get a new StreamReader from a //file with byte order mark detection = false StreamReader srFromFileFalse = new StreamReader("C:\\Temp\\Test.txt", false); //Get a new StreamReader from a file StreamReader srFromFile = new StreamReader("C:\\Temp\\Test.txt"); //Get a new StreamReader in ASCII format from a //FileStream with byte order mark detection = false and a buffer StreamReader srAsciiFromStreamFalse512 = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false, 512); //Get a new StreamReader in ASCII format from a //FileStream with byte order mark detection = false StreamReader srAsciiFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII, false); //Get a new StreamReader in ASCII format from a FileStream StreamReader srAsciiFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), System.Text.Encoding.ASCII); //Get a new StreamReader from a //FileStream with byte order mark detection = false StreamReader srFromStreamFalse = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), false); //Get a new StreamReader from a FileStream StreamReader srFromStream = new StreamReader( (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt")); }

    For more details ,you can refer to this link from MSDN :

    https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    If you use UTF-16code , you can try this code snippet:

    byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(text);
     However, .NET provides the Encoding class to allow you to decode binary data into strings, and re-encode it later.

    Encoding.Convert can convert a byte array representing text encoded with one encoding into a byte array with the same text encoded with a different encoding.

    I hope my reply do help to you .

    If you have anything else ,please feel to let me know .

    Best regards

    Kevin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, September 8, 2016 5:29 AM