C# convert string into its byte[] equivalent

后端 未结 6 1790
北恋
北恋 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 07:02

    The problem is with your approach to start with:

    I need the exact value of the bytes with no encoding

    ...

    For example if the value of the string is (0xFF32)

    That's a bit like looking at an oil painting and saying, "I want the bytes for that picture, with no encoding." It doesn't make sense. Text isn't the same as binary data. Once you understand that, it's easy to get to the root of the problem. What you really want is the contents of a file as a byte array. That's easy, because files are binary data! You shouldn't be reading it as text in the first place if it isn't really text. Fortunately, .NET makes this really easy:

    byte[] fileC = File.ReadAllBytes(dialog.FileName);
    

提交回复
热议问题