It turned out to translate the text into binary, but it does not work back.

Binary translation:

public string StringToBinary(string data) { StringBuilder sb = new StringBuilder(); foreach (char c in data.ToCharArray()) { sb.Append(Convert.ToString(c, 2)); } return sb.ToString(); } 

Back doing this:

 StringBuilder sb = new StringBuilder(); foreach (char c in cent.ToCharArray()) { sb.Append(Convert.ToString(c, 8)); } return sb.ToString(); 

In the 2nd case, the following is displayed:

616060616160616 ...

a couple of hundred lines of this.

  • 2
    This StringToBinary gets a string representation of the binary data - is that exactly what you want? There are methods for translating string->byte[] and vice versa - Encoding.UTF8.GetBytes(data); and Encoding.UTF8.GetString(data); (if you have utf8 encoding) - tym32167
  • @ tym32167, I will try to rewrite using - string-> byte [] and Encoding.UTF8.GetBytes (data); Thank you - Anton Ryabukhin
  • @ tym32167, tried and got the wrong result. I probably misunderstood you. I need to get the binary code from the string. For example, "hello" => "010101". I implemented it in 1, but it is impossible to return it back. - Anton Ryabukhin
  • All information in the computer is stored in binary code. Binary code is how information is presented. This "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
  • I want to write a translator that will convert words to code. Suppose I enter the letter "P", and in the ASCII table it is 0101000, then my "translator" displays this line consisting of 0 and 1. To be honest, I'm at a loss, because I thought that "100101" - This is a binary code and in memory only. @ tym32167 - Anton Ryabukhin

1 answer 1

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:

  1. Each byte is searched LINQ -query ( data.Select(...) )
  2. Byte is converted to a twofold code ( Convery.ToString(byt, 2) )
  3. 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'); )
  4. 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:

  • The only thing I want to add is when working with a large amount of data here ToBinaryString it is better to use StringBuilder for performance purposes. - tym32167 8:48 pm
  • @ tym32167 I agree with you, but I hope he understood the sequence of actions. - Jeidoz pm