Convert byte[] to char[]

前端 未结 3 1731
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 05:04

How do I convert a byte array to a char array in C#?

3条回答
  •  有刺的猬
    2020-12-24 05:30

    byte[] a = new byte[50];
    
    char [] cArray= System.Text.Encoding.ASCII.GetString(a).ToCharArray();
    

    From the URL thedixon posted

    http://bytes.com/topic/c-sharp/answers/250261-byte-char

    You cannot ToCharArray the byte without converting it to a string first.

    To quote Jon Skeet there

    There's no need for the copying here - just use Encoding.GetChars. However, there's no guarantee that ASCII is going to be the appropriate encoding to use.

提交回复
热议问题