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

How To Load Array Values Into Listbox & ComboBox In C#

combox tutLast 3 days I have been working on a Library Management System, it's for my college actually it's a group project, but i promised my group members that I will code and design the whole system, by the way i really had lots of problems because this would be my first time doing with a database, also I didn't know how a Library Management System works because I have never been to a Library. Hopefully I am almost done, I am going to meet my group members and ask them to make the Documentation.

Loading Array values into a ComboBox, I had to do it in my Library Management System, I will wrote the same code that I used in my Library Management System.

First we need to create a private Const int(const is a special kind of variable....), a private String Array and set value to them.
private const int Total = 13;
        private String[] genre = new String[Total];
Now coming to the function, we'll create a Function called LoadBookGenres(), and set set values to the arrays, adding to the listbox or combobox, by the way you can still go straightly without creating a new Function like you can set the values for the arrays, and load(sort) the array values to the listbox or the combobox in an event(ex:Form_Load).
private void LoadBookGenres()
        {
            //Assign value into Array
            genre[0] = "Adventure";
            genre[1] = "Animals";
            genre[2] = "Computers & Internet";
            genre[3] = "Biography";
            genre[4] = "Comics";
            genre[5] = "Horror";
            genre[6] = "Sports";
            genre[7] = "Romance";
            genre[8] = "Historical";
            genre[9] = "Music";
            genre[10] = "Religion";
            genre[11] = "Mystery";
            genre[12] = "Other";

            //Sort array and fill listbox with the sorted array
            Array.Sort(genre);
            for (int i = 0; i < genre.Length; i++)
            {
                listBox1.Items.Add(genre[i]);
            }
        }
NOTE : Change ListBox1 to your listbox name or your combobox name.
 Remember we set the public const int to 13, always in computer science you should count from 0 and upwards.
Now we just need to call the Function from an event, I called it from Form_load
private void Form1_Load(object sender, EventArgs e)
        {
            LoadBookGenres();
        }

Here is a picture of this in ListBox.
array

Here is a picture of this in ComboBox.
 
I hope this has helped you in someway, Thanks for reading.

How To Make Unicode String Reader In VB.NET

How To Make Unicode String Reader In VB.NET
hey guys , today i was searching for some old things and i found out a simple program i wrote before two years and it's a unicode string reader , it has a simple interface i have used a richtextbox and a button and of course a openfiledialog in code . when you click the button the openfiledialog dialog comes up and you just go ahead and select any file and click ok and you have the unicode string of the file in the richtextbox by the way you will laugh this program has three lines of codes :D , ok now go create a new project and add a button and change the text property to "open" and then add a rich text box and now it's time for codes , double click button and add these code


        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim data() As Byte = System.IO.File.ReadAllBytes(OpenFileDialog1.FileName)
            RichTextBox1.Text = System.Text.Encoding.Unicode.GetString(data)

now debug and try it here is a preview :

  How To Make Unicode String Reader In VB.NET

it doesn't looks like unicode or whatever