Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I fooled around with direct char arrays, and your one failing case works with my implementation. The code has been tested well: so do your tests first.</p> <p>You could speed this up by using unsafe code. But I am sure UnicodeEncoding is just as slow (if not slower).</p> <pre><code>/// &lt;summary&gt; /// Represents an encoding that packs bytes tightly into a string. /// &lt;/summary&gt; public class ByteEncoding : Encoding { /// &lt;summary&gt; /// Gets the Byte Encoding instance. /// &lt;/summary&gt; public static readonly Encoding Encoding = new ByteEncoding(); private ByteEncoding() { } public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { for (int i = 0; i &lt; chars.Length; i++) { // Work out some indicies. int j = i * 2; int k = byteIndex + j; // Get the bytes. byte[] packedBytes = BitConverter.GetBytes((short) chars[charIndex + i]); // Unpack them. bytes[k] = packedBytes[0]; bytes[k + 1] = packedBytes[1]; } return chars.Length * 2; } public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { for (int i = 0; i &lt; byteCount; i += 2) { // Work out some indicies. int j = i / 2; int k = byteIndex + i; // Make sure we don't read too many bytes. byte byteB = 0; if (i + 1 &lt; byteCount) { byteB = bytes[k + 1]; } // Add it to the array. chars[charIndex + j] = (char) BitConverter.ToInt16(new byte[] { bytes[k], byteB }, 0); } return (byteCount / 2) + (byteCount % 2); // Round up. } public override int GetByteCount(char[] chars, int index, int count) { return count * 2; } public override int GetCharCount(byte[] bytes, int index, int count) { return (count / 2) + (count % 2); } public override int GetMaxByteCount(int charCount) { return charCount * 2; } public override int GetMaxCharCount(int byteCount) { return (byteCount / 2) + (byteCount % 2); } } </code></pre> <p>Here is some test code:</p> <pre><code> static void Main(string[] args) { byte[] original = new byte[256]; // Note that we can't tell on the decode side how // long the array was if the original length is // an odd number. This will result in an // inconclusive result. for (int i = 0; i &lt; original.Length; i++) original[i] = (byte) Math.Abs(i - 1); string packed = ByteEncoding.Encoding.GetString(original); byte[] unpacked = ByteEncoding.Encoding.GetBytes(packed); bool pass = true; if (original.Length != unpacked.Length) { Console.WriteLine("Inconclusive: Lengths differ."); pass = false; } int min = Math.Min(original.Length, unpacked.Length); for (int i = 0; i &lt; min; i++) { if (original[i] != unpacked[i]) { Console.WriteLine("Fail: Invalid at a position {0}.", i); pass = false; } } Console.WriteLine(pass ? "All Passed" : "Failure Present"); Console.ReadLine(); } </code></pre> <p>The test works, but you are going to have to test it with your API function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload