Ejemplo n.º 1
0
        public static byte[] StringToBytes(string textInput)
        {
            if (string.IsNullOrEmpty(textInput))
            {
                return(null);
            }
            List <byte> bytes = new List <byte>();

            for (int i = 0; i < textInput.Length; i++)
            {
                bytes.Add(CharUtils.CharToByte(textInput[i]));
            }
            return(bytes.ToArray());
        }
Ejemplo n.º 2
0
        public static string BytesToBinary(byte[] bytes)
        {
            if (bytes == null || bytes.Length < 1)
            {
                return(string.Empty);
            }
            StringBuilder sb = new StringBuilder();

            foreach (byte b in bytes)
            {
                sb.Append(CharUtils.ByteToBinary(b));
            }
            return(sb.ToString());
        }
Ejemplo n.º 3
0
        public static byte[] BinaryToBytes(string binaryInput)
        {
            if (string.IsNullOrEmpty(binaryInput))
            {
                return(null);
            }
            string buffer;

            if (binaryInput.Length % 8 != 0)
            {
                StringBuilder sb   = new StringBuilder();
                int           left = 8 - (binaryInput.Length % 8);
                for (int i = 0; i < left; i++)
                {
                    sb.Append('0');
                }
                sb.Append(binaryInput);
                buffer = sb.ToString();
            }
            else
            {
                buffer = binaryInput;
            }
            List <byte> bytes = new List <byte>();

            for (int i = 0; i < buffer.Length; i += 8)
            {
                if (!(CharUtils.IsLegalHex(buffer[i]) && CharUtils.IsLegalHex(buffer[i + 1]) && CharUtils.IsLegalHex(buffer[i + 2]) && CharUtils.IsLegalHex(buffer[i + 3]) &&
                      CharUtils.IsLegalHex(buffer[i + 4]) && CharUtils.IsLegalHex(buffer[i + 5]) && CharUtils.IsLegalHex(buffer[i + 6]) && CharUtils.IsLegalHex(buffer[i + 7])))
                {
                    return(null);
                }
                bytes.Add(BinaryToByte(buffer.Substring(i, 8)));
            }
            return(bytes.ToArray());
        }
Ejemplo n.º 4
0
 public static byte HexToNibble(string input)
 {
     return(CharUtils.HexToNibble(input[0]));
 }
Ejemplo n.º 5
0
 public static byte StringToByte(string input)
 {
     return(CharUtils.CharToByte(input[0]));
 }