Convert byte[] to char[]

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

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

相关标签:
3条回答
  • 2020-12-24 05:26

    You must know the source encoding.

    string someText = "The quick brown fox jumps over the lazy dog.";
    byte[] bytes = Encoding.Unicode.GetBytes(someText);
    char[] chars = Encoding.Unicode.GetChars(bytes);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 05:32
    System.Text.Encoding.ChooseYourEncoding.GetString(bytes).ToCharArray();
    

    Substitute the right encoding above: e.g.

    System.Text.Encoding.UTF8.GetString(bytes).ToCharArray();
    
    0 讨论(0)
提交回复
热议问题