0

I convert string(UTF-8) to char array (in C#). and show on char array to Console, But it can not show UTF-8 Char in Console. how can i resolve this problem?

I use IDE Visual studio 2015 for C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace array
{
    class Program
    {
        #region Convert String to char array
        static void stringtochar()
        {
            string str0; 
            Console.OutputEncoding = Encoding.UTF8;
            Console.Write("Nhập Chuỗi Ký Tự :");
            str0 = Console.ReadLine();
            Char[] str_arr = new Char[100]; // Create array
            Array.Copy(str0.ToCharArray(), str_arr,str0.Length);
            for (int i = 0; i < str0.Length; i++) {
                Console.Write("[{0}] => \"{1}\"", i, str_arr[i]); }
                Console.ReadLine();
        }
        #endregion
        static void Main(string[] args)
        {
            stringtochar();
        }
    }
}



Nhập Chuỗi Ký Tự :đi chơi [0] => "d"[1] => "i"[2] => " "[3] => "c"[4] => "h"[5] => "o"[6] => "i"

i expect output to be [0] => "đ"

Thanh Lam
  • 1
  • 2
  • Possible duplicate of [How to write Unicode characters to the console?](https://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console) – jason.kaisersmith Jun 21 '19 at 14:10

1 Answers1

0

Your string is in unicode. So change the encoding to unicode and also add input encoding as well

Console.OutputEncoding = Encoding.Unicode;

Console.InputEncoding = Encoding.Unicode;
Anil Goel
  • 261
  • 1
  • 8