Convert byte array to string C#, VB.Net

Byte array to string

Byte array to string C# vb.Net

In .Net, every string has a character set and encoding. A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters. Actually it is the process of transforming a set of Unicode characters into a sequence of bytes.

More about.... Character Encoding

When you try to convert a Byte Array object to String, you still have a character set and encoding and it depends on the encoding of your string whether its is in ASCII or UTF8. It is important to note that you should use the same encoding for both directions You can use Encoding.GetString Method (Byte[]) to decodes all the bytes in the specified byte array into a string.

Using UTF8 conversion

Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

Using ASCII conversion

Encoding.ASCII .GetString(byteArray, 0, byteArray.Length);
UTF8 Conversion Example
using System; using System.Data; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string testString = "Hello World !!"; //Here converting the string to byteArray byte[] byteArray = Encoding.UTF8.GetBytes(testString); //Here converting the byteArray to string string str = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length); MessageBox.Show(str); } } }
ASCII Conversion Example
using System; using System.Data; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string testString = "Hello World !!"; //Here converting the string to byteArray byte[] byteArray = Encoding.ASCII.GetBytes(testString); //Here converting the byteArray to string string str = Encoding.ASCII.GetString(byteArray, 0, byteArray.Length); MessageBox.Show(str); } } }

Convert String to ByteArray

Convert String to ByteArray C# vb.Net

When you try to convert a String object to Byte Array, you still have a character set and encoding and it depends on the encoding of your string whether its is in ASCII or UTF8. So you can use System.Text.Encoding.Unicode.GetBytes() to retrieve the set of bytes that Microsoft.Net would using to represent the characters. More about...... String to byte Array

Stream to ByteArray

The Streams Object involve three fundamental operations such as Reading, Writing and Seeking. In some situations we may need to convert these stream to byte array. More about...... Byte Array from Stream

Image to Byte Array

Image to Byte Array C# vb.Net

In many situations you may forced to convert image to byte array. It is useful in many scenarios because byte arrays can be easily compared, compressed, stored, or converted to other data types. More about...... Image to Byte Array



NEXT.....What is an IP Address ?