C# || How To Convert A String To Byte Array & Byte Array To String Using C#
The following is a module with functions which demonstrates how to convert a string to a byte array and a byte array to a string using C#.
1. String To Byte Array
The example below demonstrates the use of ‘Utils.Extensions.GetBytes‘ to convert a string to a byte array. The optional parameter allows to specify the encoding.
1 2 3 4 5 6 7 8 |
// String To Byte Array using Utils; var text = $"My Programming Notes!"; var bytes = text.GetBytes(); // Do something with the byte array |
2. Byte Array To String
The example below demonstrates the use of ‘Utils.Extensions.GetString‘ to convert a byte array to a string. The optional parameter allows to specify the encoding.
1 2 3 4 5 6 7 8 9 |
// Byte Array To String using Utils; var text = $"My Programming Notes!"; var bytes = text.GetBytes(); var str = bytes.GetString(); // Do something with the string |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 8, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Extensions { /// <summary> /// Decodes all bytes in the specified byte array into a string /// </summary> /// <param name="bytes">The byte array containing the sequence of bytes to decode</param> /// <param name="encode">The type of encoding to be used. Default is UTF8Encoding</param> /// <returns> A byte array containing the results of encoding</returns> public static string GetString(this byte[] bytes, System.Text.Encoding encode = null) { if (encode == null) { encode = new System.Text.UTF8Encoding(); } return encode.GetString(bytes); } /// <summary> /// Encodes the characters in the specified string into a sequence of bytes /// </summary> /// <param name="str">The string containing the characters to encode</param> /// <param name="encode">The type of encoding to be used. Default is UTF8Encoding</param> /// <returns> A byte array containing the results of encoding</returns> public static byte[] GetBytes(this string str, System.Text.Encoding encode = null) { if (encode == null) { encode = new System.Text.UTF8Encoding(); } return encode.GetBytes(str); } } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 8, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { static void Main(string[] args) { try { var text = $"My Programming Notes!"; var bytes = text.GetBytes(); var str = bytes.GetString(); Display(str); Display($"{str == text}"); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply