Пример #1
0
        private static String ConvertToString(byte[] bytes, HashFormat hashFormat)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(null);
            }

            switch (hashFormat)
            {
            case HashFormat.Base64:
                return(Convert.ToBase64String(bytes));

            case HashFormat.Binary:
            {
                var sb = new StringBuilder();

                foreach (var b in bytes)
                {
                    var bin = Convert.ToString(b, 2).PadLeft(8, '0');
                    sb.Append(bin);
                }

                return(sb.ToString());
            }

            case HashFormat.Bytes:
            {
                return(String.Join(",", bytes));
            }

            case HashFormat.Dec:
            {
                var binaryNumber = ConvertToString(bytes, HashFormat.Binary);
                return(NumberBaseConverter.ChangeBase(binaryNumber, 2, 10));
            }

            case HashFormat.Hex:
            {
                using (var writer = new StringWriter())
                {
                    writer.Write("0x");

                    foreach (var b in bytes)
                    {
                        writer.Write("{0:x2}", b);
                    }

                    return(writer.ToString());
                }
            }
            }

            return(null);
        }