Esempio n. 1
0
        /// <summary>
        /// Converts decimal string to bytes.
        /// </summary>
        /// <param name="description">The decimal string.</param>
        /// <returns>The converted bytes.</returns>
        /// <remarks><c>" 16 18 "</c> is converted to <c>new byte[] { 0x10, 0x12 }</c>.</remarks>
        public static byte[] ConvertDecimal(string description)
        {
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }

            var result  = new List <byte> ();
            var content = description.Trim().Split(new[] { ' ' });

            foreach (var part in content)
            {
#if CF
                result.Add(byte.Parse(part, NumberStyles.Integer, CultureInfo.InvariantCulture));
#else
                byte temp;
#if NETCF
                if (TryParsers.ByteTryParse(part, out temp))
#else
                if (byte.TryParse(part, out temp))
#endif
                { result.Add(temp); }
#endif
            }

            return(result.ToArray());
        }
Esempio n. 2
0
        /// <summary>
        /// Converts the byte string to bytes.
        /// </summary>
        /// <param name="description">The HEX string.</param>
        /// <returns>The converted bytes.</returns>
        /// <remarks><c>"80 00"</c> is converted to <c>new byte[] { 0x80, 0x00 }</c>.</remarks>
        public static byte[] Convert(IEnumerable <char> description)
        {
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }

            var result = new List <byte> ();
            var buffer = new StringBuilder(2);

            foreach (var c in description.Where(c => !char.IsWhiteSpace(c)))
            {
                if (!char.IsLetterOrDigit(c))
                {
                    throw new ArgumentException("illegal character found", "description");
                }

                buffer.Append(c);
                if (buffer.Length != 2)
                {
                    continue;
                }
#if CF
                result.Add(byte.Parse(buffer.ToString(), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture));
                #else
                byte temp;
#if NETCF
                if (TryParsers.ByteTryParse(buffer.ToString(), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp))
#else
                if (byte.TryParse(buffer.ToString(), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out temp))
#endif
                { result.Add(temp); }
#endif
                buffer.Length = 0;
            }

            if (buffer.Length != 0)
            {
                throw new ArgumentException("not a complete byte string", "description");
            }

            return(result.ToArray());
        }