Sai A Sai A
Updated date May 15, 2023
In this blog, we will explain three different methods for converting binary decoded strings to normal strings in C# and provides code examples for each method.

Introduction:

In computer science, a string is a sequence of characters, which can be either text or binary data. Binary data is often represented as a string of ones and zeroes. When we receive or store binary data, it is sometimes necessary to convert it into a normal string to make it human-readable. In this blog post, we will explore how to convert binary decoded strings to normal strings in C#.

Method 1: Using Encoding.ASCII.GetString()

One way to convert binary decoded strings to normal strings in C# is to use the Encoding.ASCII.GetString() method. This method takes a byte array that represents the binary data and returns a string.

byte[] binaryData = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };
string normalString = Encoding.ASCII.GetString(binaryData);
Console.WriteLine(normalString);

Output:

Hello World

In this example, we create a byte array called binaryData that represents the binary data we want to convert. We then use the Encoding.ASCII.GetString() method to convert the binary data into a string and store it in the normalString variable. Finally, we print the normalString variable to the console, which outputs "Hello World".

Method 2: Using Convert.ToBase64String() and Convert.FromBase64String()

Another way to convert binary decoded strings to normal strings in C# is to use the Convert.ToBase64String() and Convert.FromBase64String() methods. These methods allow us to convert binary data to and from base64-encoded strings.

byte[] binaryData = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };
string base64String = Convert.ToBase64String(binaryData);
byte[] decodedBinaryData = Convert.FromBase64String(base64String);
string normalString = Encoding.ASCII.GetString(decodedBinaryData);
Console.WriteLine(normalString);

Output:

Hello World

In this example, we create a byte array called binaryData that represents the binary data we want to convert. We then use the Convert.ToBase64String() method to convert the binary data to a base64-encoded string and store it in the base64String variable. We then use the Convert.FromBase64String() method to convert the base64-encoded string back into binary data and store it in the decodedBinaryData variable. Finally, we use the Encoding.ASCII.GetString() method to convert the binary data into a string and store it in the normalString variable. We print the normalString variable to the console, which outputs "Hello World".

Method 3: Using Bitwise Operations

A third way to convert binary decoded strings to normal strings in C# is to use bitwise operations. We can use the bitwise OR operator (|) to combine two bytes into a single char, and then append the char to a string.

byte[] binaryData = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 };
string normalString = "";
for (int i = 0; i < binaryData.Length; i += 2)
 {
  char c = (char)(binaryData[i] | binaryData[i + 1] << 8);
  normalString += c;
 }
Console.WriteLine(normalString);

Output:

Hello World

In this example, we create a byte array called `binaryData` that represents the binary data we want to convert. We then create an empty string called `normalString`. We loop through the `binaryData` array two bytes at a time, combining each pair of bytes into a single char using the bitwise OR operator and then appending the char to the `normalString` variable. Finally, we print the `normalString` variable to the console, which outputs "Hello World".

Conclusion:

In this blog post, we explored three different methods for converting binary decoded strings to normal strings in C#. The first method used the Encoding.ASCII.GetString() method, the second method used the Convert.ToBase64String() and Convert.FromBase64String() methods, and the third method used bitwise operations.

Comments (0)

There are no comments. Be the first to comment!!!