C# Convert String, Byte Array

Convert or change

A string can be converted into a byte array. Strings are stored with two bytes per character. ASCII only allows one byte per character. This can cause data loss.
With Encoding.ASCII.GetBytes,
and GetString,
we perform this conversion.

Example

String type

First, we look at a program that uses a constant string literal and then converts that to a byte array. Please note that the GetBytes method will cause an incorrect conversion if the input string is not actually ASCII.

String Literal

Then:In the final loop, we examine each byte of the resulting byte array. We see its numeric and character-based representation.

Foreach Loop ExamplesByte Type
Program that uses Encoding.ASCII.GetBytes method: C#

using System;
using System.Text;

class Program
{
    static void Main()
    {
	// Input string.
	const string input = "Dot Net Perls";

	// Invoke GetBytes method.
	// ... You can store this array as a field!
	byte[] array = Encoding.ASCII.GetBytes(input);

	// Loop through contents of the array.
	foreach (byte element in array)
	{
	    Console.WriteLine("{0} = {1}", element, (char)element);
	}
    }
}

Output

68 = D
111 = o
116 = t
32 =
78 = N
101 = e
116 = t
32 =
80 = P
101 = e
114 = r
108 = l
115 = s

In this program, the character representations from the input string were first converted to fit in one byte elements each. Then, we see that the integer 68 corresponds to the uppercase letter "D". This is the standard.

Example 2

Byte type

Next, a byte array can be converted into a string. First, this program allocates an array of bytes. These represent characters in ASCII. Next the ASCIIEncoding.ASCII.GetString method is used.

Tip:You will need to ensure System.Text is included to compile the program. Please add the using System.Text directive.

Program that converts byte array to string: C#

using System;
using System.Text;

class Program
{
    static void Main()
    {
	byte[] array =
	{
	    68,
	    111,
	    116,
	    32,
	    78,
	    101,
	    116,
	    32,
	    80,
	    101,
	    114,
	    108,
	    115
	};

	string value = ASCIIEncoding.ASCII.GetString(array);
	Console.WriteLine(value);
    }
}

Output

Dot Net Perls
Array type

We see that the byte array does not need to contain the "\0" character to be converted to a string correctly. The space character in the example is encoded as the byte 32. We provide more information on the char type.

Char

Memory

Framework: NET

How can you use the Encoding.ASCII.GetBytes method to optimize the memory usage in your C# program? Let's say you have some string caches in your program and these must be resident in memory for some time.

Tip:Instead of leaving them as strings, you can convert them to byte arrays (if they are ASCII). This will occupy half the memory.

Byte Array

Also:The memory reduction will improve performance of garbage collection and decrease its frequency, by improving memory locality.

Garbage CollectionLocality of Reference

Summary

Framework: NET

It is possible to convert strings and byte arrays using the C# language. Please note that this conversion may cause some data loss if you are using some characters that are not in the ASCII character set.

And:Because of this, be careful when using this method in places where not necessary.


C#: Cast