DQDQbJA%{PPy({g9UUy{WU@8pU'y88yyyyyyyyyyyDQDQbJA%UbPhyOU'h(aAabeEeBABDXZCY| 649G54VCWC~B 7VCBCBX

String, byte array.` A string can be converted into a byte array. Strings are stored with two bytes per character. ASCII only allows one byte per character.`Conversion problems.` With some ASCII conversions, we can lose data. With Encoding.ASCII.GetBytes, and GetString, we perform this conversion.`First example.` 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. `String Literal `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 `foreach`Byte `byte`Program notes.` 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.` 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. `You will need to ensure System.Text is included to compile the program. Please add the using System.Text directive.`We see that the byte array does not need to contain the "\0" character to be converted to a string correctly.`Space: `The space character in the example is encoded as the byte 32. We provide more information on the char type.`Char `char`Memory.` 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. `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 `byte-array`The memory reduction will improve performance of garbage collection and decrease its frequency, by improving memory locality.`Locality of Reference `locality-of-reference`A summary.` 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.

OQR QQ; QQ.TextQQQ { OQO{R OOQInput Q. OORconst Q inputQR"Q"R;R OOQInvoke GetBytes mQ. OOQYou can store this Q as a field! OORbyte[] QQREncoding.ASCII.GetBytesR(input);R OOQQthrough contents of the Q. OORQR (byte QQQ) OO{ OOOQR"{0}Q{1}"R, Q, (char)Q); OO} O} } R 68QD 111Qo 116Qt 32 = 78QN 101Qe 116Qt 32 = 80QP 101Qe 114Qr 108Ql 115QsR QQ; QQ.TextQQQ { OQO{ OObyte[] Q = OO{ OOO68, OOO111, OOO116, OOO32, OOO78, OOO101, OOO116, OOO32, OOO80, OOO101, OOO114, OOO108, OOO115 OO}QOOQ QQASCIIEncoding.ASCII.RGetQR(Q); OOQQ); O} } R QR

"n=Encoding.ASCII.GetBytes methodconverts byte array to string