If I understand you correctly, then you need to translate your text into a binary line, and then the binary line itself back into the text. To translate text into a double view, you need to know the encoding of your text (UTF8 is enough in most cases) and translate the text into a byte array with the desired encoding using the System.Text.Encoding.UTF8.ToBytes(myString);
method System.Text.Encoding.UTF8.ToBytes(myString);
. After that, each byte must be translated into a double string:
public static string ToBinaryString(byte[] data) { return string.Join("", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0'))); }
In this method, the following happens:
- Each byte is searched LINQ -query (
data.Select(...)
) - Byte is converted to a twofold code (
Convery.ToString(byt, 2)
) - A prefix in the form of zeros is added to the converted byte if the double line is less than 8 characters (
().PadLeft(8, '0');
) - And all double strings are joined into one by the
string.Join("", strings);
method string.Join("", strings);
where "" is a separator between the lines (in our case it is not needed, so the empty line).
Translation from a binary string to a regular one occurs in the reverse order. First we get the bytes:
public static byte[] ToByteArray(string binaryString) { int numOfBytes = binaryString.Length / 8; byte[] bytes = new byte[numOfBytes]; for (int i = 0; i < numOfBytes; i++) { string oneBinaryByte = binaryString.Substring(8 * i, 8); bytes[i] = Convert.ToByte(oneBinaryByte, 2); } return bytes; }
Since in one byte there are 8 bits, then in the cycle we take substrings of 8 characters each and translate into bytes. Then the method Encoding.UTF8.GetString(bytesOfNewString);
translate into a string.
Here's an example with all the code:
public static string ToBinaryString(byte[] data) { return string.Join("", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0'))); } public static byte[] ToByteArray(string binaryString) { int numOfBytes = binaryString.Length / 8; byte[] bytes = new byte[numOfBytes]; for (int i = 0; i < numOfBytes; i++) { string oneBinaryByte = binaryString.Substring(8 * i, 8); bytes[i] = Convert.ToByte(oneBinaryByte, 2); } return bytes; } static void Main(string[] args) { string originString = "Мой супер текст!"; Console.WriteLine($"Первоначальная строка: '{originString}'"); byte[] bytesOfString = Encoding.UTF8.GetBytes(originString); string binaryString = ToBinaryString(bytesOfString); Console.WriteLine($"Двоечная строка: '{binaryString}'"); byte[] bytesOfNewString = ToByteArray(binaryString); string newOriginString = Encoding.UTF8.GetString(bytesOfNewString); Console.WriteLine($"Конвертированя строка из двоечной: '{newOriginString}'"); Console.ReadKey(); }
Code examples are taken from answers to specific topics:
StringToBinary
gets a string representation of the binary data - is that exactly what you want? There are methods for translatingstring->byte[]
and vice versa -Encoding.UTF8.GetBytes(data);
andEncoding.UTF8.GetString(data);
(if you have utf8 encoding) - tym32167"010101"
is not a binary code, it is a string of characters of ones and zeros. You probably should explain what you are doing and why, otherwise you will not understand what you need. - tym32167 6:19 pm