public void When_ToHexString1()
        {
            byte[] source   = new byte[] { 0, 255, 255, 0 };
            string expected = "00-FF-FF-00";
            string actual;

            actual = ByteExtensions.ToHexString(source, '-');

            Assert.AreEqual(expected, actual, true);
        }
        public void When_ToHexString()
        {
            byte   source   = 255;
            string expected = "FF";
            string actual;

            actual = ByteExtensions.ToHexString(source);

            Assert.AreEqual(expected, actual, true);
        }
Beispiel #3
0
        /// <summary>Gets an object complete representation as a string, with all its properties.</summary>
        /// <param name="obj">The object to describe.</param>
        /// <param name="byteArrayEncoding">Optional encoding to decode byte arrays, if null write them using hexadecimal.</param>
        /// <param name="typeFullName">If true, uses the fully qualified name for types, otherwise just the name.</param>
        /// <returns>The object represented as a string.</returns>
        public string ToFullString(object obj, Encoding byteArrayEncoding = null, bool typeFullName = false)
        {
            if (obj == null)
            {
                return("");
            }

            if (obj is DateTime)
            {
                return(((DateTime)obj).ToString("o"));
            }

            if (ShouldUseObjectMethods(obj))
            {
                return(obj.ToString());
            }

            if (obj is byte[])
            {
                if (byteArrayEncoding == null)
                {
                    return(ByteExtensions.ToHexString((byte[])obj));
                }
                return(byteArrayEncoding.GetString((byte[])obj));
            }

            if (obj is IEnumerable)
            {
                return(String.Format("[{0}]", String.Join(", ", ((IEnumerable)obj).Cast <object>()
                                                          .Select <object, string>(child => ToFullString(child, byteArrayEncoding, typeFullName)).ToArray())));
            }

            Type type = obj.GetType();
            IEnumerable <string> properties = type.GetProperties().Select(property => String.Format("{0}: {1}",
                                                                                                    property.Name, ToFullString(property.GetValue(obj, null), byteArrayEncoding, typeFullName)));

            return(String.Format("{0} ({1})", typeFullName ? type.FullName : type.Name, String.Join(", ", properties.ToArray())));
        }