C#NET使用指定的字符集将缓冲区编码为字符串

        /// <summary>
        ///     使用指定的字符集将缓冲区编码为字符串。
        /// </summary>
        /// <param name="buff">输入缓冲区</param>
        /// <param name="charset">字符集.</param>
        /// <returns>编码字符串.</returns>
        public static string EncodeString(byte[] buff, char[] charset) {
            int current = buff[0];
            var ret = new StringBuilder();
            for (int i = 1; i < buff.Length; i++) {
                current = (current << 8) + buff[i];
                while (current >= charset.Length) {
                    ret.Append(charset[current % charset.Length]);
                    current /= charset.Length;
                }
            }
            if (current != 0)
                ret.Append(charset[current % charset.Length]);
            return ret.ToString();
        }

猜你喜欢

转载自www.cnblogs.com/wz2988/p/12416528.html