Convert binary string representation to byte array

How do you convert a string such as "01110100011001010111001101110100" to an array of bytes, then use File.WriteAllBytes so that the exact binary string is a binary file. In this case, it will be the text "test".

+23
c # binary
Aug 08 '10 at
source share
5 answers

If you don’t have this LINQ fetish so common lately, you can try it in the usual way.

string input .... int numOfBytes = input.Length / 8; byte[] bytes = new byte[numOfBytes]; for(int i = 0; i < numOfBytes; ++i) { bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2); } File.WriteAllBytes(fileName, bytes); 

LINQ is great, but there should be some limitations.

+28
Aug 08 '10 at 23:21
source share

You can start by breaking a string into a sequence of 8-character strings, then converting these strings to bytes, and finally writing 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 a string into 8-character chunks, maybe a little easier:

 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 string length 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)); 
+9
Aug 08 2018-10-10T00:
source share

A bit late, but here are my 2 cents:

 var binaryStr = "01110100011001010111001101110100"; var byteArray = Enumerable.Range(0, int.MaxValue/8) .Select(i => i*8) .TakeWhile(i => i < binaryStr.Length) .Select(i => binaryStr.Substring(i, 8)) .Select(s => Convert.ToByte(s, 2)) .ToArray(); File.WriteAllBytes("C:\temp\test.txt", byteArray); 
+3
Aug 09 '10 at 9:23 a.m.
source share

You wrote other answers, but just for fun, I wrote the opposite. Going from string to 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; } 
0
Aug 08 '10 at 23:18
source share

Actually the answer from @Maciej is incorrect. Since @ Phate01 noticed that numOfBytes only valid for an input length that is 8 power. Secondly, the byte array must be filled from n to 0 index, not vice versa. Here is a sample code:

 var bits = "000011110000001000"; var numOfBytes = (int)Math.Ceiling(bits.Length / 8m); var bytes = new byte[numOfBytes]; var chunkSize = 8; for (int i = 1; i <= numOfBytes; i++) { var startIndex = bits.Length - 8 * i; if (startIndex < 0) { chunkSize = 8 + startIndex; startIndex = 0; } bytes[numOfBytes - i] = Convert.ToByte(bits.Substring(startIndex, chunkSize), 2); } 

This can be improved to get rid of the statetment if , but in this form it is more clear.

0
Nov 04 '15 at 11:23
source share



All Articles