0

I am exporting data to CSV which may be in Japanese or English. Currently I am exporting it using Encoder UTF8Encoding.

I can see data in Japanese in the CSV file after exporting, but now I want to import this data to my application. Do I need to decode this? While importing I am getting data in the form of digits or other characters.

Veedrac
  • 58,273
  • 15
  • 112
  • 169
Payal S
  • 11
  • 2
  • Thats a really Good Question! I have the same querry as well. – swapnil Jun 10 '14 at 08:50
  • What encoding do you use during import? Show your export/import code... – st4hoo Jun 10 '14 at 08:54
  • That's *not* a good question for here... it's clearly *not* a programming problem. It is a business decision. All the same, see [Which is the best character encoding for Japanese language for DB, php, and html display?](http://stackoverflow.com/questions/1045338/which-is-the-best-character-encoding-for-japanese-language-for-db-php-and-html) and [Japanese language and computers](http://en.wikipedia.org/wiki/Japanese_language_and_computers). The consensus is to use UTF-8. – Sheridan Jun 10 '14 at 08:54
  • Can you show the code..and your efforts? – jiten Jun 10 '14 at 08:55
  • could you show your piece of code when encoding /decoding? – Yuliam Chandra Jun 10 '14 at 08:59
  • @YuliamChandra while exporting I am getting bytes like this and writing these bytes to CSV file. byte[] buffer =System.Text.UTF8Encoding.Unicode.GetBytes(sb.ToString()); where sb is string which is data I am exporting. What should I use while importing? – Payal S Jun 10 '14 at 09:12

2 Answers2

2

Yes, you need to decode the same way when you encoded it.

class Program
{
    static void Main(string[] args)
    {
        var writeText = "你叫什么名字?";
        var writeBytes = System.Text.UTF8Encoding.Unicode.GetBytes(writeText);
        File.WriteAllBytes("D:\\test.txt", writeBytes);

        var readBytes = File.ReadAllBytes("D:\\test.txt");
        var readText = System.Text.UTF8Encoding.Unicode.GetString(readBytes);
        Console.WriteLine(readText);
    }
}
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
1

Of course, you need to decode it.

Try something like this:

private string Decoder(string originFilePath)
{
   using (var sr = new StreamReader(originFilePath, Encoding.GetEncoding("utf-8")))
   {
      return sr.ReadToEnd();
   }
}
Phoenix
  • 1,045
  • 1
  • 14
  • 22