<p>I would like to put a string into a byte array, but the string may be too big to fit.  In the case where it's too large, I would like to put as much of the string as possible into the array.  Is there an efficient way to find out how many characters will fit?</p>

[{'answer_id': 34431, 'author': 'otsdr', 'author_id': 321238, 'author_profile': 'https://Stackoverflow.com/users/321238', 'pm_score': 4, 'selected': True, 'text': '<p>In order to truncate a string to a UTF8 byte array without splitting in the middle of a character I use this:</p>\n\n<pre><code>static string Truncate(string s, int maxLength) {\n    if (Encoding.UTF8.GetByteCount(s) &lt;= maxLength)\n        return s;\n    var cs = s.ToCharArray();\n    int length = 0;\n    int i = 0;\n    while (i &lt; cs.Length){\n        int charSize = 1;\n        if (i &lt; (cs.Length - 1) &amp;&amp; char.IsSurrogate(cs[i]))\n            charSize = 2;\n        int byteSize = Encoding.UTF8.GetByteCount(cs, i, charSize);\n        if ((byteSize + length) &lt;= maxLength){\n            i = i + charSize;\n            length += byteSize;\n        }\n        else\n            break;\n    }\n    return s.Substring(0, i);\n}\n</code></pre>\n\n<p>The returned string can then be safely transferred to a byte array of length maxLength.</p>\n'}, {'answer_id': 34442, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>You should be using the Encoding class to do your conversion to byte array correct?  All Encoding objects have an overridden method GetMaxCharCount, which will give you "The maximum number of characters produced by decoding the specified number of bytes."  You should be able to use this value to trim your string and properly encode it.</p>\n'}, {'answer_id': 34448, 'author': 'skolima', 'author_id': 3205, 'author_profile': 'https://Stackoverflow.com/users/3205', 'pm_score': 1, 'selected': False, 'text': '<p>Efficient way would be finding how much (pessimistically) bytes you will need per character with</p>\n\n<pre><code>Encoding.GetMaxByteCount(1);\n</code></pre>\n\n<p>then dividing your string size by the result, then converting that much characters with</p>\n\n<pre><code>public virtual int Encoding.GetBytes (\n string s,\n int charIndex,\n int charCount,\n byte[] bytes,\n int byteIndex\n)\n</code></pre>\n\n<p>If you want to use less memory use</p>\n\n<pre><code>Encoding.GetByteCount(string);\n</code></pre>\n\n<p>but that is a much slower method.</p>\n'}, {'answer_id': 34450, 'author': 'Joseph Daigle', 'author_id': 507, 'author_profile': 'https://Stackoverflow.com/users/507', 'pm_score': 1, 'selected': False, 'text': '<p>The Encoding class in .NET has a method called <code>GetByteCount</code> which can take in a string or char[]. If you pass in 1 character, it will tell you how many bytes are needed for that 1 character in whichever encoding you are using.</p>\n\n<p>The method <code>GetMaxByteCount</code> is faster, but it does a worst case calculation which could return a higher number than is actually needed.</p>\n'}]
['https://Stackoverflow.com/questions/34395', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2348/']