TryParseHexString() public static method

Tries to parse a hex string to a byte array.
public static TryParseHexString ( string s, byte &bytes ) : bool
s string The hex string.
bytes byte A byte array.
return bool
示例#1
0
        /// <summary>
        /// Tries to parse a string and create a new ObjectId.
        /// </summary>
        /// <param name="s">The string value.</param>
        /// <param name="objectId">The new ObjectId.</param>
        /// <returns>True if the string was parsed successfully.</returns>
        public static bool TryParse(string s, out ObjectId objectId)
        {
            if (s != null && s.Length == 24)
            {
                byte[] bytes;
                if (BsonUtils.TryParseHexString(s, out bytes))
                {
                    objectId = new ObjectId(bytes);
                    return(true);
                }
            }

            objectId = default(ObjectId);
            return(false);
        }
示例#2
0
        /// <summary>
        /// Tries to parse a string and create a new ObjectId.
        /// </summary>
        /// <param name="s">The string value.</param>
        /// <param name="objectId">The new ObjectId.</param>
        /// <returns>True if the string was parsed successfully.</returns>
        public static bool TryParse(string s, out ObjectId objectId)
        {
            // don't throw ArgumentNullException if s is null
            if (s != null && s.Length == 24)
            {
                byte[] bytes;
                if (BsonUtils.TryParseHexString(s, out bytes))
                {
                    objectId = new ObjectId(bytes);
                    return(true);
                }
            }

            objectId = default(ObjectId);
            return(false);
        }
示例#3
0
        /// <summary>
        /// Tries to parse a string and create a new ObjectId.
        /// </summary>
        /// <param name="s">The string value.</param>
        /// <param name="objectId">The new ObjectId.</param>
        /// <returns>True if the string was parsed successfully.</returns>
        public static bool TryParse(string s, out ObjectId objectId)
        {
            // don't throw ArgumentNullException if s is null
            if (s != null && s.Length == 24)
            {
                Span <byte> bytes = stackalloc byte[BsonUtils.GetHexStringBinaryLength(s)];
                if (BsonUtils.TryParseHexString(s, bytes))
                {
                    objectId = new ObjectId(bytes);
                    return(true);
                }
            }

            objectId = default(ObjectId);
            return(false);
        }
示例#4
0
        /// <summary>
        /// Tries to parse a string and create a new ObjectId.
        /// </summary>
        /// <param name="s">The string value.</param>
        /// <param name="objectId">The new ObjectId.</param>
        /// <returns>True if the string was parsed successfully.</returns>
        public static bool TryParse(string s, out ObjectId objectId)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }
            if (s.Length == 24)
            {
                byte[] bytes;
                if (BsonUtils.TryParseHexString(s, out bytes))
                {
                    objectId = new ObjectId(bytes);
                    return(true);
                }
            }

            objectId = default(ObjectId);
            return(false);
        }