Convert a binary string representation to a byte array?

In case you don't have this LINQ fetish, so common lately, you can try the normal way string input .... int numOfBytes = input. Length / 8; byte bytes = new bytenumOfBytes; for(int I = 0; I Substring(8 * i, 8), 2); } File. WriteAllBytes(fileName, bytes) LINQ is great but there must be some limits.

In case you don't have this LINQ fetish, so common lately, you can try the normal way string input .... int numOfBytes = input. Length / 8; byte bytes = new bytenumOfBytes; for(int I = 0; I Substring(8 * i, 8), 2); } File. WriteAllBytes(fileName, bytes); LINQ is great but there must be some limits.

Much more understandable – jwsample Aug 8 '10 at 23:27 This assumes that the string length is a multiple of 8... But you're right, it is probably easier to understand that way. I guess I'm totally addicted to LINQ ;) – Thomas Levesque Aug 8 '10 at 23:30 Hmm. I'd argue that the LINQ is more understandable.It describes concisely what is being done without describing the (noisy) steps to acheive that goal.

It has a much higher content to noise ratio. – spender Aug 9 '10 at 0:04 @spender Sure LINQ is a nice tool for uses it was meant for, but for this simple and straightforward case? Come on, Thomas's code is longer, creates at least 4 enumerable objects, one intermediate array of strings, 5 delegates, and an anonymous reference type for every digit '0' or '1'.

This is insane. – Maciej Hehl Aug 9 '10 at 17:48.

You could start by splitting the string into a sequence of 8-character strings, then convert those strings to bytes, and eventually write the bytes to a file string input = "01110100011001010111001101110100"; var bytesAsStrings = input. Select((c, i) => new { Char = c, Index = I }) . GroupBy(x => x.

Index / 8) . Select(g => new string(g. Select(x => x.

Char).ToArray())); byte bytes = bytesAsStrings. Select(s => Convert. ToByte(s, 2)).ToArray(); File.

WriteAllBytes(fileName, bytes); EDIT: here's another way to split the string into 8-character chunks, perhaps a bit simpler : int nBytes = (int)Math. Ceiling(input. Length / 8m); var bytesAsStrings = Enumerable.

Range(0, nBytes) . Select(i => input. Substring(8 * i, Math.

Min(8, input. Length - 8 * i))); If you know that the length of the string is a multiple of 8, you can make it even simpler : int nBytes = input. Length / 8; var bytesAsStrings = Enumerable.

Range(0, nBytes) . Select(i => input. Substring(8 * i, 8)).

Just bumped into this question. A nice toy problem... good for an interview. I thought I'd give it a shot before reading the answers.

Matched your second approach virtually term for term. I too am addicted to LINQ! +1 – spender Aug 9 '10 at 0:06.

The other answers have you covered, but just for fun I wrote the opposite. Going from the string to the ascii binary representation: private static string StringToAsciiBin(string s) { string output = ""; foreach (char c in s.ToCharArray()) { for (int I = 128; I >= 1; I /=2) { if (((int)c & i) > 0) { output += "1"; } else { output += "0"; } } } return output; }.

You would want to go from a byte array to the binary represenation. A char is a 16 bit data type, so you would chop off the top eight bits of each character code. Also, don't use += to build a string, it scales terribly badly, use a StringBuilder instead.

– Guffa Aug 8 '10 at 23:36.

A bit late, but here's my 2 cents: var binaryStr = "01110100011001010111001101110100"; var byteArray = Enumerable. Range(0, int. MaxValue/8) .

Select(i => i*8) . TakeWhile(i => I binaryStr. Substring(i, 8)) .

Select(s => Convert. ToByte(s, 2)) .ToArray(); File. WriteAllBytes("C:\temp\test.

Txt", byteArray).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions