C# convert string into its byte[] equivalent

后端 未结 6 1792
北恋
北恋 2021-01-05 06:10

At this point most people will be thinking \"Ah ill post this..:\"

byte[] dataB= System.Text.Encoding.ASCII.GetBytes(data);

However.. the p

6条回答
  •  情歌与酒
    2021-01-05 06:41

    However.. the problem I have is i need the exact value of the bytes with no encoding just the pure value for each byte.

    Then use this:

    byte[] dataB = System.Text.Encoding.Unicode.GetBytes(data);
    

    It returns the bytes as stored internally by .NET strings.

    But all this is codswallop: A string is always linked to a particular encoding and there's no way around it. The above will fail e.g. if the file contains invalid Unicode code sequences (which may happen) or through normalization. Since you obviously don't want a string, don't read one. Read the file as binary data instead.

提交回复
热议问题