5

I have a string which contains binary data (non-text data).

How do I convert this to a raw byte array?

3

4 Answers 4

9

A string in C# - by definition - does not contain binary data. It consists of a sequence of Unicode characters.


If your string contains only Unicode characters in the ASCII (7-bit) character set, you can use Encoding.ASCII to convert the string to bytes:

byte[] result = Encoding.ASCII.GetBytes(input);

If you have a string that contains Unicode characters in the range u0000-u00ff and want to interpret these as bytes, you can cast the characters to bytes:

byte[] result = new byte[input.Length];
for (int i = 0; i < input.Length; i++)
{
    result[i] = (byte)input[i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok. So I probably should have a String in the first place. Thanks.
5

It is a very bad idea to store binary data in a string. However, if you absolutely must do so, you can convert a binary string to a byte array using the 1252 code page. Do not use code page 0 or you will lose some values when in foreign languages. It just so happens that code page 1252 properly converts all byte values from 0 to 255 to Unicode and back again.

There have been some poorly written VB6 programs that use binary strings. Unfortunately some are so many lines of code that it is almost impossible to convert it all to byte() arrays in one go.

You've been warned. Use at your own peril:

Dim bData() As Byte
Dim sData As String

'convert string binary to byte array
bData = System.Text.Encoding.GetEncoding(1252).GetBytes(sData)

'convert byte array to string binary
sData = System.Text.Encoding.GetEncoding(1252).GetString(bData)

6 Comments

You can't use CP1252 for '\x80' character, it converts to '?'.
byte x80 converts to Unicode value x20ac, and back again. How did you get a question mark?
Maybe it depends on system. I used BitConverter.ToString(Encoding.GetEncoding(1252).GetBytes("\x80")) and got 3F. You can try it on ideone.com
The BitConverter.ToString() is not actually codepage 1252. It returned me the string "80" as two separate characters "8" and "0". I'm talking about storing actual binary in a string type. For example, this will turn x80 from a byte to a string, and then back to a byte: MessageBox.Show(Encoding.GetEncoding(1252).GetByte(Encoding.GetEncoding(1252).GetString(128))))
Mistake on that line, and couldn't edit my comment. It should read: MessageBox.Show(Encoding.GetEncoding(1252).GetBytes(Encoding.GetEncoding(1252).GetString(New Byte() {128}))(0))
|
1

Here is one way:

public static byte[] StrToByteArray(string str)
{
  System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
  return encoding.GetBytes(str);
}

2 Comments

Won't the ASCII encoding stuff garble my data?
@Joel: ASCIIEncoding works if your input contains only characters in the range u0000 - u007f. It will throw an exception if the input string contains any other character.
0
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] theBytes = encoding.GetBytes("Some String");

Note, there are other encoding formats you may want to use.

1 Comment

won't work. Contents of source is assumed in UTF-16, which will fail on some byte value combinations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.