Example #1
0
        public override int GetByteCount(char[] chars, int index, int count)
        {
            int byteCount = 0;

            try
            {
                if (chars == null)
                {
                    throw new ArgumentNullException("chars");
                }
                if (index < 0 || index > chars.Length)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                if (count < 0 || count > (chars.Length - index))
                {
                    throw new ArgumentOutOfRangeException("count");
                }

                for (int i = index; i < count; i++)
                {
                    if (CharToByte.ContainsKey(chars[i]))
                    {
                        byteCount += CharToByte[chars[i]].Length;
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }
            return(byteCount);
        }
Example #2
0
        public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
        {
            int byteCount = 0;

            // Validate the parameters.
            if (chars == null)
            {
                throw new ArgumentNullException("chars");
            }
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException("charIndex");
            }
            if (charCount < 0 || charCount > (chars.Length - charIndex))
            {
                throw new ArgumentOutOfRangeException("charCount");
            }
            if (byteIndex < 0 || byteIndex > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex");
            }
            if (byteIndex + GetByteCount(chars, charIndex, charCount) > bytes.Length)
            {
                throw new ArgumentException("bytes array too small", "bytes");
            }
            for (int i = charIndex; i < charIndex + charCount; i++)
            {
                byte[] charByte;
                if (CharToByte.TryGetValue(chars[i], out charByte))
                {
                    charByte.CopyTo(bytes, byteIndex + byteCount);
                    byteCount += charByte.Length;
                }
            }
            return(byteCount);
        }