How to convert byte[] array to string and Vice versa in C#?


Hello, I would like to know how to convert byte[] array to string and string to byte[] array in C#, using the best possible method, I know there are several links and methods available on the internet but I would like to know best and easy method for this, thanks.


Asked by:- neena
1
: 12425 At:- 6/14/2018 11:13:41 AM
C# byte array to string c# byte[] to string c# convert byte array to string convert byte[] to string c#







3 Answers
profileImage Answered by:- vikas_jk

You can use following methods to convert byte[] to string or string to byte[] in C#, let's take a look on it, one by one:

Converting byte array into string

Method 1 (When you don't know encoding):

static string ByteArrayToStringConverted(byte[] bytes)
{
    using (var stream = new MemoryStream(bytes))
    {
        using (var streamReader = new StreamReader(stream))
        {
            return streamReader.ReadToEnd();
        }
    }
}

Method 2 (Using StringBuilder):

public static string ByteArrayToString(byte[] ba) {
  StringBuilder stringHex = new StringBuilder(ba.Length * 2);
  foreach(byte b in ba)
  {
   stringHex.AppendFormat("{0:x2}", b);
  }
 return stringHex.ToString();
}

Method 3 (Encoding's GetString):

             string testString = "Hello World !!";
            //Here converting the string to byteArray
            byte[] byteArray = Encoding.UTF8.GetBytes(testString);
            //converting the byteArray to string
            string str = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

in the above code we are doing UTF 8 Conversion

Method 4 (BitConverter.ToString):

string strhex = BitConverter.ToString(ba);
return strhex.Replace("-","");

Method 5 (Convert.ToBase64String):

string s3 = Convert.ToBase64String(bytes);

Converting string to byte array

Method 1:

public static byte[] ConvertStringToByteArray(String stringHex) {
  int NumbChars = stringHex.Length;
  byte[] bytes = new byte[NumbChars / 2];
  for (int i = 0; i < NumbChars; i += 2)
  {
    bytes[i/2] = Convert.ToByte(stringHex.Substring(i, 2), 16);
  }
  return bytes;
}

Method 2:

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

Method 3:

byte[] bytes = Encoding.UTF8.GetBytes(MainString);

That's it, hope it helps

2
At:- 6/17/2018 12:56:53 PM Updated at:- 9/27/2022 8:39:08 AM
Thanks 0
By : neena - at :- 6/18/2018 12:56:04 PM


profileImage Answered by:- vikas_jk

Dotnet Fiddle example to convert byte array into string using C#

using System;
					
public class Program
{
	public static void Main()
	{
	    byte[] bytearray = new byte[] {97,114,114,97,121,32,101,120,97,109,112,108,101,115 };
		//method1
        string txt = System.Text.ASCIIEncoding.ASCII.GetString(bytearray);

        //method2
        string text2 = System.Text.Encoding.Default.GetString(bytearray);
		
		Console.WriteLine(txt);
		Console.WriteLine(text2);
	}
}

Output;

array examples
array examples
1
At:- 12/23/2019 10:54:57 AM
This is the simplest one to convert byte array to string and string to byte array in C# 0
By : pika - at :- 6/3/2022 10:52:17 AM


profileImage Answered by:- bhanu

BitConverter can be used to convert byte[] to string using C#, as it is very simple to use

var convertedStr = BitConverter.ToString(YourBytesArray);

Namespace "System" is used when using BitConverter.

Complete sample code

using System;
					
public class Program
{
	public static void Main()
	{
		 const string formatter = "{0,25}{1,30}";

        double  aDoubl  = 0.1111111111111111111;
		Console.WriteLine( formatter, aDoubl,
        BitConverter.ToString( BitConverter.GetBytes( aDoubl ) ) );
		
	}
}

Output:

 0.111111111111111       1C-C7-71-1C-C7-71-BC-3F
1
At:- 4/10/2021 3:12:39 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use