String
, byte
arrayA string
can be converted into a byte
array. Strings are stored with two bytes per character. ASCII only allows one byte
per character.
With some ASCII conversions, we can lose data. With Encoding.ASCII.GetBytes
, and GetString
, we perform this conversion.
This program uses a constant string
literal and then converts that to a byte
array. The GetBytes
method will cause an incorrect conversion if the input string is not ASCII.
byte
elements each.byte
of the resulting byte
array. We see its numeric and character-based representation.using System; using System.Text; // Input string. const string input = "abc!"; // 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); }97 = a 98 = b 99 = c 33 = !
A byte
array can be converted into a string
. This program allocates an array of bytes. These represent characters in ASCII. Next the ASCIIEncoding.ASCII.GetString
method is used.
System.Text
is included to compile the program. Please add the using System.Text
directive.byte
array does not need to contain the "\0" character to be converted to a string
correctly.byte
32. We provide more information on the char
type.using System; using System.Text; byte[] array = { 68, 111, 116, 32, 78, 101, 116, 32, 80, 101, 114, 108, 115 }; string value = ASCIIEncoding.ASCII.GetString(array); Console.WriteLine(value);Dot Net Perls
Suppose we want to "compress" ASCII strings in memory. We can convert strings to byte
arrays with no loss of data, and this reduces total memory usage.
string
into a byte
array. And the memory of this array is measured.string[]
required about 480,000 bytes. The byte[]
[] (a jagged array of byte
arrays) required 320,000 bytes.byte
arrays back into strings by calling ASCIIEncoding.ASCII.GetString
.using System; using System.IO; using System.Text; class Program { static void Main() { // Version 1: get string array. long a = GC.GetTotalMemory(true); string[] array = Get(); long b = GC.GetTotalMemory(true); array[0] = null; // Version 2: get byte arrays. long c = GC.GetTotalMemory(true); byte[][] array2 = Compress(Get()); long d = GC.GetTotalMemory(true); array2[0] = null; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); } static string[] Get() { string[] output = new string[10000]; for (int i = 0; i < 10000; i++) { output[i] = Path.GetRandomFileName(); } return output; } static byte[][] Compress(string[] array) { byte[][] output = new byte[array.Length][]; for (int i = 0; i < array.Length; i++) { output[i] = ASCIIEncoding.ASCII.GetBytes(array[i]); } return output; } }39128 479800 39784 320056
It is possible to convert strings and byte
arrays. This conversion may cause some data loss if you are using some characters that are not in the ASCII character set.
Because of problems, be careful when using this method in places where not necessary. There is a performance cost to these conversions.