示例#1
0
        //CREDIT goes to some guy on stackoverflow.com  ups..
        public static byte[] HexStringToBytes(string s)
        {
            const string HEX_CHARS = "0123456789ABCDEF";

            if (s.Length == 0)
            {
                return(new byte[0]);
            }

            if ((s.Length + 1) % 3 != 0)
            {
                throw new FormatException();
            }

            byte[] bytes = new byte[(s.Length + 1) / 3];

            int state       = 0; // 0 = expect first digit, 1 = expect second digit, 2 = expect hyphen
            int currentByte = 0;
            int x;
            int value = 0;

            foreach (char c in s)
            {
                switch (state)
                {
                case 0:
                    x = HEX_CHARS.IndexOf(Char.ToUpperInvariant(c));
                    if (x == -1)
                    {
                        throw new FormatException();
                    }
                    value = x << 4;
                    state = 1;
                    break;

                case 1:
                    x = HEX_CHARS.IndexOf(Char.ToUpperInvariant(c));
                    if (x == -1)
                    {
                        throw new FormatException();
                    }
                    bytes[currentByte++] = (byte)(value + x);
                    state = 2;
                    break;

                case 2:
                    if (c != '-')
                    {
                        throw new FormatException();
                    }
                    state = 0;
                    break;
                }
            }

            return(bytes);
        }
示例#2
0
        public void SetPatternBytes(string s)
        {
            const string HEX_CHARS = "0123456789ABCDEF";

            if (s.Length == 0)
                this.Pattern = new byte[] { 0 };

            if ((s.Length + 1) % 3 != 0)
                throw new FormatException();

            byte[] bytes = new byte[(s.Length + 1) / 3];

            int state = 0; // 0 = expect first digit, 1 = expect second digit, 2 = expect hyphen
            int currentByte = 0;
            int x;
            int value = 0;

            foreach (char c in s)
            {
                switch (state)
                {
                    case 0:
                        x = HEX_CHARS.IndexOf(Char.ToUpperInvariant(c));
                        if (x == -1)
                            throw new FormatException();
                        value = x << 4;
                        state = 1;
                        break;
                    case 1:
                        x = HEX_CHARS.IndexOf(Char.ToUpperInvariant(c));
                        if (x == -1)
                            throw new FormatException();
                        bytes[currentByte++] = (byte)(value + x);
                        state = 2;
                        break;
                    case 2:
                        if (c != ' ')
                            throw new FormatException();
                        state = 0;
                        break;
                }
            }

            this.Pattern = bytes;
        }