<p>While there are 100 ways to solve the conversion problem, I am focusing on performance.</p>

<p>Give that the string only contains binary data, what is the fastest method, in terms of performance, of converting that data to a byte[] (not char[]) under C#?</p>

<p>Clarification:  This is not ASCII data, rather binary data that happens to be in a string.</p>

[{'answer_id': 72191, 'author': 'user7116', 'author_id': 7116, 'author_profile': 'https://Stackoverflow.com/users/7116', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.getbytes.aspx" rel="nofollow noreferrer">UTF8Encoding.GetBytes</a></p>\n'}, {'answer_id': 72500, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': "<p>There is <em>no such thing</em> as an ASCII string in C#! Strings <em>always</em> contain UTF-16. Not realizing this leads to a lot of problems. That said, the methods mentioned before work because they consider the string as UTF-16 encoded and transform the characters to ASCII symbols.</p>\n\n<p>/EDIT in response to the clarification: how did the binary data get in the string? Strings aren't supposed to contain binary data (use <code>byte[]</code> for that).</p>\n"}, {'answer_id': 72822, 'author': 'Davy Landman', 'author_id': 11098, 'author_profile': 'https://Stackoverflow.com/users/11098', 'pm_score': 3, 'selected': True, 'text': '<p>I\'m not sure ASCIIEncoding.GetBytes is going to do it, because it only supports the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.asciiencoding" rel="nofollow noreferrer">range 0x0000 to 0x007F</a>. </p>\n\n<p>You tell the string contains only bytes. But a .NET string is an array of chars, and 1 char is 2 bytes (because a .NET stores strings as UTF16). So you can either have two situations for storing the bytes 0x42 and 0x98:</p>\n\n<ol>\n<li>The string was an ANSI string and contained bytes and is converted to an unicode string, thus the bytes will be 0x00 0x42 0x00 0x98. (The string is stored as 0x0042 and 0x0098)</li>\n<li>The string was just a byte array which you typecasted or just recieved to an string and thus became the following bytes 0x42 0x98. (The string is stored as 0x9842)</li>\n</ol>\n\n<p>In the first situation on the result would be 0x42 and 0x3F (ascii for "B?"). The second situation would result in 0x3F (ascii for "?"). This is logical, because the chars are outside of the valid ascii range and the encoder does not know what to do with those values.</p>\n\n<p>So i\'m wondering why it\'s a string with bytes?</p>\n\n<ul>\n<li>Maybe it contains a byte encoded as a string (for instance <a href="https://en.wikipedia.org/wiki/Base64" rel="nofollow noreferrer">Base64</a>)?</li>\n<li>Maybe you should start with an char array or a byte array?</li>\n</ul>\n\n<p>If you realy do have situation 2 and you want to get the bytes out of it you should use the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.unicodeencoding.getbytes" rel="nofollow noreferrer">UnicodeEncoding.GetBytes</a> call. Because that will return 0x42 and 0x98.</p>\n\n<p>If you\'d like to go from a char array to byte array, the fastest way would be Marshaling.. But that\'s not really nice, and uses double memory. </p>\n\n<pre><code>public Byte[] ConvertToBytes(Char[] source)\n{\n    Byte[] result = new Byte[source.Length * sizeof(Char)];\n    IntPtr tempBuffer = Marshal.AllocHGlobal(result.Length);\n    try\n    {\n        Marshal.Copy(source, 0, tempBuffer, source.Length);\n        Marshal.Copy(tempBuffer, result, 0, result.Length);\n    }\n    finally\n    {\n        Marshal.FreeHGlobal(tempBuffer);\n    }\n    return result;\n}\n</code></pre>\n'}, {'answer_id': 74121, 'author': 'bzlm', 'author_id': 7724, 'author_profile': 'https://Stackoverflow.com/users/7724', 'pm_score': 0, 'selected': False, 'text': '<p>If you want to go from a string to binary data, <strong>you must know what encoding was used to convert the binary data to a string in the first place</strong>. Otherwise, you might not end up with the correct binary data. So, the most efficient way is likely GetBytes() on an Encoding subclass (such as UTF8Encoding), but you must know for sure which encoding.</p>\n\n<p>The comment by Kent Boogaart on the original question sums it up pretty well. ;]</p>\n'}]
['https://Stackoverflow.com/questions/72176', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12113/']