Playing With Strings And Bytes/Byte - Arrays C#

Introduction

 
Most developers I’ve encountered when playing with strings and/or bytes are always interested to see what is really inside the physical device. They always imagine, “What’s really inside?” Even I sometimes ask myself, “Do we really need to see the ones and zeroes?”
 
Basically, you can play with bytes, bits and strings and see the represented byte-string. In this article will try to explore the different ways to manipulate byte-arrays to string and string to byte-arrays. Lastly, we are going to tackle a bit about the “Encodings”, and focus more on the methods such as “GetyBytes”,“GetByteCount” and “BitConverter”.
 

Background

 
So before we play with strings and bytes. I want to introduce you a summary basic concept of ASCII and Unicode. Here are some lists to take note of,
  • ASCII (American Standard Code for Information Interchange) and Unicode are used for communication, wherein a computer can possibly transfer data from one computer to another.
  • ASCII uses 7 bits to represent a character and have been extended to 8 bits “extended ASCII” which solves the Latin alphabet while Unicode represents more languages in the world. That’s wherein the “Unicode Encoding” comes into play because most characters don’t fit into the 8-bits size.
  • Unicode encodings come into play because we need numerous ways to store a character in a byte sequence. See the different type of Encodings: UTF8, UTF-16, and UTF-32.
For more information about the difference of ASCII and Unicode, please see my LinkedIn article.
 

.NET Encoding

 
Before we start with the examples, I would like to introduce you to the “System.Text.Encoding”. It is basically an abstract class which is intended to represent a character encoding. It also provides methods to convert arrays to strings of Unicode characters to and from arrays of bytes. Here are the list of the derived classes,
  • ASCIIEncoding
  • UTF7Encoding
  • UTF8Encoding
  • UnicodeEncoding
  • UTF32Encoding
See the figure 1 below to visualize the hierarchy of the inheritance.
 
Playing With Strings And Bytes/Byte - Arrays C#
Figure 1
 
If you want to programmatically get the derived encoding classes, see the sample code below,
  1. [TestMethod]  
  2. public void Test_Types_Of_Derived_Encoding_Classes()  
  3. {  
  4.     var type = typeof(Encoding);  
  5.     var assembly = Assembly.GetAssembly(type);  
  6.     var types = assembly.GetTypes();  
  7.   
  8.     var derivedClasses =   
  9. types.Where(t => t.IsSubclassOf(type) &&  t.IsPublic == true).ToList();  
  10.   
  11.     foreach (var @class in derivedClasses)  
  12.     {  
  13. Console.WriteLine(@class.Name);  
  14.     }  
  15. }  
Just some notes to keep in mind, when using those properties to use different encodings from the abstract class “System.Text.Encoding” such as ASCII, UTF8, UTF7, etc. It actually creates a new instance of that derived class. Please see the code below,
 
Playing With Strings And Bytes/Byte - Arrays C# 
 
Note
The code is based from here.
 
Therefore you can create a new instance of a certain encoding type or you can use the abstract class and choose a property-encoding type specific for your needs.
  1. var utf8 = new UTF8Encoding(); //you can use this   
  2. var utf8_2 = Encoding.UTF8; //or you can use this  
Let us try to see if the concept is true, see the code example below:
  1. [TestMethod]  
  2. public void Test_If_Encodings_Are_Same_Type() {  
  3.     Assert.IsInstanceOfType(Encoding.ASCII, typeof(ASCIIEncoding)); //true  
  4.     Assert.IsInstanceOfType(Encoding.UTF7, typeof(UTF7Encoding)); //true  
  5.     Assert.IsInstanceOfType(Encoding.UTF8, typeof(UTF8Encoding)); //true  
  6.     Assert.IsInstanceOfType(Encoding.Unicode, typeof(UnicodeEncoding));  
  7.     //true  
  8.     Assert.IsInstanceOfType(Encoding.UTF32, typeof(UTF32Encoding)); //true  
  9. }  

String to Byte Array

 
In order to convert string to byte array you need a specific Encoding, then use the “GetBytes” method. As it converts a string into byte array let us also see the character and its equivalent numerical ASCII/Unicode value. Just a note using the ASCII-encoding uses 7 bits while UTF8-encoding uses 8 bits to represent a character.
 
See the example below,
  1. string strRandomWords = "I Love C#";  
  2. [TestMethod]  
  3. public void Test_ASCII_Using_GetBytes()  
  4. {  
  5. //converts a string into byte array  
  6. var byteResults = Encoding.ASCII.GetBytes(this.strRandomWords);               
  7. Assert.IsTrue(byteResults.Length > 0); //true  
  8.             #region iterate   
  9.             foreach (var @byte in byteResults)  
  10.             {  
  11.                 string fullResultInString =   
  12. string.Format("Character: {0} in ASCII {1}",   
  13. (char)@byte, @byte) ;  
  14.   
  15.                 Console.WriteLine(fullResultInString);  
  16.             }  
  17.        #endregion   
  18.  }  
Now if  you are interested also to get the byte size you then can use “GetByteCount” method. In our example we get the number of bytes depending on the encoding type. I decided to double check if the expected bits are correct. See the two examples below,
  1. string strRandomWords = "I Love C#";  
  2. [TestMethod]  
  3. public void Test_ASCII_Using_GetByteCount() {  
  4.     //converts a string into byte array  
  5.     var byteResults = Encoding.ASCII.GetBytes(this.strRandomWords);  
  6.     //get the byte count  
  7.     int byteCount = Encoding.ASCII.GetByteCount(this.strRandomWords);  
  8.     int totalBits = 0;  
  9.     for (int counter = 0; counter < byteResults.Length; counter++) {  
  10.         string bits = Convert.ToString(byteResults[counter], 2);  
  11.         totalBits = bits.Length + totalBits;  
  12.     }  
  13.     //let’s check if they are equal. 7 is used because ASCII uses 7 bits  
  14.     Assert.AreEqual(byteCount, Math.Ceiling((totalBits / 7.00)));  
  15. }  
  16. //use non Latin alphabet  character to test UTF-8  
  17. string strRandomNonEnglishStrings = "プログラミングが大好き";  
  18. [TestMethod]  
  19. public void Test_UTF8_Encoding_Using_GetByteCount() {  
  20.     //converts a string into byte array          
  21.     var byteResults = Encoding.UTF8.GetBytes(this.strRandomNonEnglishStrings);  
  22.     //get the byte count              
  23.     int byteCount = Encoding.UTF8.GetByteCount(this.strRandomNonEnglishStrings);  
  24.     int totalBits = 0;  
  25.     for (int counter = 0; counter < byteResults.Length; counter++) {  
  26.         string bits = Convert.ToString(byteResults[counter], 2);  
  27.         totalBits = bits.Length + totalBits;  
  28.     }  
  29.     //let’s check if they are equal. 8 is used because UTF8 uses 8 bits  
  30.     Assert.AreEqual(byteCount, Math.Ceiling((totalBits / 8.00)));  
  31. }   

Byte-Array to String

 
From the previous examples, this shows how we can get the byte-array from a string. Now, we can try to see, how those byte-arrays are represented as a string and see what does the byte-array actually represent in a human readable format.
 
To see a series of bytes we can then use “BitConverter,” a helper class which helps developers to convert data-types to array-types and array of bytes to base data types. Let us see some examples below,
  1. string strRandomWords = "I Love C#";  
  2. [TestMethod]  
  3. public void Test_Convert_String_To_Bytes_Formatted() {  
  4.         var bytes = Encoding.UTF8.GetBytes(strRandomWords);  
  5.         Assert.IsNotNull(bytes);  
  6.         //converts a byte array into a series of byte-strings  
  7.         var seriesOfByteStrings = BitConverter.ToString(bytes);  
  8.         Assert.IsTrue(!string.IsNullOrWhiteSpace(seriesOfByteStrings));  
  9.         Console.WriteLine(seriesOfByteStrings); //49-20-4C-6F-76-65-20-43-23  
  10.     }  
  11.     [TestMethod]  
  12. public void Test_Convert_To_Bytes_Formatted_Using_Other_Value_Types() {  
  13.     int bday = 03291982;  
  14.     var result = BitConverter.GetBytes(bday);  
  15.     Assert.IsNotNull(result);  
  16.     var seriesOfBytesStrings = BitConverter.ToString(result);  
  17.     Assert.IsTrue(!string.IsNullOrWhiteSpace(seriesOfBytesStrings));  
  18.     Console.WriteLine(seriesOfBytesStrings); //4E-3B-32-00  
  19. }  
Lastly, we can use GetString to get the exact human readable format. Let us see the example below:
  1. string strRandomWords = "I Love C#";  
  2. [TestMethod]          
  3. public void Test_ASCII_Using_Get_String()  
  4. {  
  5.    var byteResults = Encoding.ASCII.GetBytes(this.strRandomWords); //converts a string into byte array  
  6.    Assert.IsTrue(byteResults.Length > 0); //true  
  7.    string humanReadableString =   
  8.    Encoding.ASCII.GetString(byteResults, 0, byteResults.Length);  
  9.    Assert.AreEqual(humanReadableString, strRandomWords);  
  10. }  

Summary

 
In this article, we have explored a brief concept of ASCII & Unicode. We have also seen that “System.Text.Encoding” does have derived classes such as ASCIIEncoding, UTF7Encoding, UTF8Encoding, UnicodeEncoding & UTF32Encoding. Upon learning those derived classes you may choose to use the Encoding.[Encoding-Type] e.g. Encoding.ASCII or creating a new instance e.g. var asci = new ASCIIEncoding().After that we have focused on the conversion of String to Byte array and vice versa. 
 
By the way, most the source code samples are also available on GitHub. I really did enjoy creating this article, I’m hoping you felt the same way too as you read it. Until next time, happy programming. 


Recommended Free Ebook
Similar Articles