예제 #1
0
        private static ArrayLease <char> TranslateBytesToHex(ArraySegment <byte> bytes, out int charCount)
        {
            byte[] buffer = bytes.Array;
            int    offset = bytes.Offset;

            charCount = bytes.Count * 2;
            var lease = ArrayPool <char> .Shared.Lease(charCount);

            try
            {
                var chars = lease.Array;

                int  j = 0;
                char left, right;

                for (int i = 0; i < bytes.Count; i++)
                {
                    byte b = buffer[offset + i];
                    Hexadecimal.FromByte(b, out left, out right);
                    chars[j]     = left;
                    chars[j + 1] = right;
                    j           += 2;
                }

                Debug.Assert(j == charCount);
            }
            catch
            {
                lease.Dispose();
                throw;
            }

            return(lease);
        }