In this blog we see how to convert byte array to string using following methods:
1. Convert byte array to string using Convert.ToBase64String
This method is used to convert the specified byte array to its corresponding string format which is encoded with base-64 digits.
It is basically used to store and transfer the data for a media files such as image files. It uses a characters defined in encoding set to display a data.
Syntax:
public static string ToBase64String(byte[] Array)
t accepts one parameter "Array" which specifies a array bytes.
It returns a corresponding string in base64 of specified array bytes.
Exceptions:
This method throws only "ArgumentNullException" if value parameter is null.
namespace ConvertByteToString
{
class Program
{
static void Main(string[] args)
{
byte[] arrayBytes = { 100, 222, 194, 03 };
string str = Convert.ToBase64String(arrayBytes);
Console.WriteLine("String: " + str);
}
}
}
Output:
String : ZN7CAw==
2. Convert byte array to string using HttpServerUtility.UrlTokenEncode(Byte[])
This method is used to convert or encode the specified byte array to its corresponding string format by using base-64 digits. Basically, it is used for the transmission safe data on the URL. This method defined in HttpServerUtility class. This class provides a various methods to handle a web requests.
Syntax:
public static string UrlTokenEncode(byte[] bytes)
It accepts one parameter "bytes" which specifies a array bytes.
It returns a encoded string correspond to the specified array bytes.
Exceptions:
This method throws only "ArgumentNullException" if value parameter is null.
To convert byte array to string using this method, see the below code:
namespace ConvertByteToString
{
class Program
{
static void Main(string[] args)
{
byte[] arrayBytes = { 102, 101, 103 };
string str = HttpServerUtility.UrlTokenEncode(arrayBytes );
Console.WriteLine("String: " + str);
}
}
}
Output:
String : ZmVn0
0 Comment(s)