How to Convert Byte Array to String in C#

In .NET, a byte is just a number from 0 to 255 (the numbers that can be represented by eight bits). So, a byte array is just an array of the numbers from 0 to255. At a lower level, an array is a contiguous block of memory, and a byte array is just a representation of that memory in 8-bit blocks.

Let's say you have a Byte[] array loaded from a file and you need to convert it to a String.

1. Encoding's GetString
but you won't be able to get the original bytes back if those bytes have non-ASCII characters

byte[] bytes = { 130, 200, 234, 23 }; // A byte array contains non-ASCII (or non-readable) characters
string Enco = Encoding.UTF8.GetString(bytes); 
byte[] decBytes1 = Encoding.UTF8.GetBytes(Enco);  // decBytes1.Length == 10 !!
// decBytes1 not same as bytes
// Using UTF-8 or other Encoding object will get similar results

2. BitConverter.ToString
The output is a "-" delimited string, but there's no .NET built-in method to convert the string back   to byte array.

string Bitconvo = BitConverter.ToString(bytes);   // 82-C8-EA-17
String[] tempAry = Bitconvo.Split('-');
byte[] decBytes2 = new byte[tempAry.Length];
for (int i = 0; i < tempAry.Length; i++)
    decBytes2[i] = Convert.ToByte(tempAry[i], 16);
// decBytes2 same as bytes

3. Convert.ToBase64String
You can easily convert the output string back to byte array by using Convert.FromBase64String.
Note: The output string could contain '+', '/' and '='. If you want to use the string in a URL, you need to explicitly encode it.

string B64 = Convert.ToBase64String(bytes);  
byte[] decByte3 = Convert.FromBase64String(B64);
// decByte3 same as bytes

4. HttpServerUtility.UrlTokenEncode
You can easily convert the output string back to byte array by using HttpServerUtility.UrlTokenDecode. The output string is already URL friendly! The downside is it needs System.Web assembly if your project is not a web project.

string s3 = Convert.ToBase64String(bytes);  // gsjqFw==
byte[] decByte3 = Convert.FromBase64String(s3);
// decByte3 same as bytes

Credits : combo_ci

Merits and Demerits of Arrays

Array is an important data structure. We can define it by saying that it is a collection of like objects. The objects are called elements which are present at contiguous memory locations. Every element of array is accessed by using its unique index. Indices of arrays always start from zero.

Merits of Array:

1: Ease of declaration:
    Arrays provide us a handy way to declare variables. We don’t have to declare as many variables as values are being stored. We just declare an array and give it a size.  For example, if you need to declare 10 variables of integer type then you can use an array. You will not have to declare ten different variables. You just declare an array of size 10.

int array[10];
Above C++ statement will allocate ten memory locations for array variable.

2: Ease of access:
    The main advantage of array is that we can access a random element. In other data structures like linked list, we have to traverse the list to find a specific element. But in array, we just give the specific index of element and use it. It saves a lot of time and memory.

3: Contiguous memory locations:

    The elements of array are stored at consecutive memory locations. For example, if we have an array of ten elements then these ten elements will be stored consecutively in the memory. These consecutive locations help to access an element quickly.

4: Implementing data structures:

    Other data structures like stacks, queues, trees etc are widely used. Arrays provide us such functionality that we can use these data structures easily.

5: Matrices Re presentation:
    Arrays are not just one-dimensional arrays. 2-D (two dimensional) arrays are also present which help us to represent matrices.

Demerits of Array:

1: Wastage of memory:
    The biggest drawback of array is that it is fixed length. Once you have declared it, you cannot change its size at run-time. For example, if you are asked to design a program for data entry and you don’t know the exact number of records to be entered. Then you will probably declare an array of maximum size you can imagine according to your problem. Now, if user enters records less than size of array then empty memory spaces would be wasted. So, this can cause wastage of memory.
       
2: Similar data type:
    You can store only those elements in array which are similar in data type. Actually when you declare an array then you also tell compiler its data type. In linked list, you can store data of more than one type in a node. So, similar data type can be considered as a drawback of array.

3: Consecutive memory locations:
    Though consecutive memory locations are useful for fast accessing of elements of an array. But this consecutiveness also makes hard the deletion and insertion of elements. For example, if you delete an element from array then you will have to traverse the array to adjust the places of remaining elements. Traversing of array is a difficult and time-consuming job. This thing also occurs in case of inserting an element in array.



About Author:

Kamal Choudhary is a tech geek who writes about C++ programming tutorials on C++ Beginner. He is a student of computer science in University of Gujrat, pakistan. He loves to write about computer programming. You can find his full bio here. Follow him on twitter @ikamalchoudhary