Technology Programming

Differences Between Byte Array Vs. String

String Data Types


After adding a new text box to a Microsoft .NET form written in C#, you can access its Text property and read the control's value. This value is a string data type and consists of Unicode characters. Unlike integers other numerical data types, strings are "reference" types. They represent objects that point data rather than the data itself. Extract a string variable's characters using the SubString method as in the following example:

string x = "1234";
string y = x.SubString(0, 2);

This SubString method returns "12," the first two characters stored in the variable named x.

Byte Data Types


Videos, text files, games and everything else on a computer consists of bytes. A byte is a computer unit made up of smaller units called bits. In C#, a byte represents an integer value between 0 and 255. The first statement below assigns 100 to a byte variable named byte1:

byte1 = 100;
char char1 = 'A';
byte1 = (byte)char1;

Chars are also Unicode characters that can also hold character data such as letters. The final two statements assign the letter "A" to a char variable and convert it into a byte.

Byte Arrays


Even though chars, bytes and strings are different data types, you can convert between them easily. A byte array is an array that has a byte type declaration, as shown below:

byte[] byteArray1;

These arrays often consist of raw binary data that might exist in an image or even a PDF. If your application retrieves such data over the Web, it arrives in binary format and may wind up in a byte array. Your application can use the byte array to reconstruct the item it retrieved, manipulate the data or store it in a database.

Converting Strings to Byte Arrays


Byte arrays also have the ability to hold string data. Convert a string variable into a byte array, as shown in the example below:

string string2 = "This is a string";
byte[] byteArray1= System.Text.Encoding.ASCII.GetBytes(string2);

The Text.Encoding.ASCII property allows the code to transform Unicode characters into ASCII. The GetBytes method converts the string into a byte array and stores it in the byteArray1 variable.

Tips


You can loop through the elements in a byte array and examine them as seen in the following example:

foreach (byte byteItem in byteArray1)
{
MessageBox.Show("Numeric value=" + byteItem + " Character value=" + (char)byteItem);
}

This code iterates through the byte array 16 times because the original string contains 16 characters. A message box appears each time the code loops and displays the numerical value of the current data item and its character representation. Looping through a byte array is an excellent way to examine the numerical ASCII values of all the characters in a string.

Related posts "Technology : Programming"

Differences Between Byte Array Vs. String

Programming

Web Design Company UK/

Programming

Develop A Quality Plan With These Self Help Tips

Programming

TMediaPlayer: What track am I on?

Programming

How You Can Prove Your Expertise

Programming

Furniture Advice And Tips And Also Hardwearing. Property Searching Wonderful

Programming

Web Design Fort Lauderdale Business

Programming

Hire Web Designer and Diminish Development Expenditure

Programming

Web Masters.

Programming

Leave a Comment