Beispiel #1
0
 public static byte[] GetBytes(this ulong @ulong, HexOptions options)
 {
     byte[] bytes = BitConverter.GetBytes(@ulong);
     if (options.IsEndianBig())
     {
         Array.Reverse(bytes);
     }
     return(bytes);
 }
Beispiel #2
0
 public static byte[] GetBytes(this ulong? @ulong, HexOptions options)
 {
     byte[] array = @ulong.HasValue ? BitConverter.GetBytes(@ulong.Value) : null;
     if (options.IsEndianBig())
     {
         Array.Reverse(array);
     }
     return(array);
 }
Beispiel #3
0
        private static void CheckOptions(HexOptions options)
        {
            if (!options.HasFlagFast(HexOptions.HEX))
            {
                string msg = String.Format("\"{0}\" must have the \"{1}\" flag set",
                                           nameof(options), nameof(HexOptions.HEX));

                throw new ArgumentException(msg, nameof(options));
            }
        }
Beispiel #4
0
        private static string ToHexInternal(long l, HexOptions options = DEFAULT)
        {
            CheckOptions(options);

            string s = l.ToString("X");

            if (options.HasFlagFast(HexOptions.ZERO_PAD_HEX) && s.Length == 1)
            {
                s = StringConstants.ZERO + s;
            }

            if (options.HasFlagFast(HexOptions.PREFIX_HEX))
            {
                s = StringConstants.HEX_PREFIX + s;
            }

            return(s);
        }
Beispiel #5
0
        public static bool TryCreateHex <T>(T value, out string s, HexOptions options = DEFAULT)
        {
            string str = String.Empty;

            if (value is Pointer)
            {
                var ptr = Pointer.Unbox(value);
                s = ToHex(ptr);
                return(true);
            }

            if (Int64.TryParse(value.ToString(), out long l))
            {
                str = ToHexInternal(l, options);
            }

            s = str;
            return(!String.IsNullOrWhiteSpace(str));
        }
Beispiel #6
0
        // todo: WIP
        private static string CreateString(IEnumerable value, string delim, HexOptions options)
        {
            // Ignore strings
            if (value is string s)
            {
                return(s);
            }

            var sb = new StringBuilder();

            foreach (var element in value)
            {
                var elemType = element.GetType();
                if (elemType.IsEnumerableType())
                {
                    var    elemObj = (IEnumerable)element;
                    string elemStr = CreateString(elemObj, delim, options);

                    if (elemType != typeof(string))
                    {
                        sb.Append('[')
                        .Append(elemStr)
                        .Append(']');
                    }
                    else
                    {
                        sb.Append(elemStr);
                    }
                }
                else
                {
                    sb.Append(CreateStringElement(element, options));
                }

                sb.Append(delim);
            }

            return(sb.RemoveTrailingDelimeter(delim).ToString());
        }
Beispiel #7
0
 public static string ToHex(this uint? @uint, HexOptions options)
 {
     return(new HexString(@uint.GetBytes(options), options));
 }
Beispiel #8
0
 public static string ToHex(this ushort @ushort, HexOptions options)
 {
     return(new HexString(@ushort.GetBytes(options), options));
 }
Beispiel #9
0
 public static string ToHex(this sbyte? @sbyte, HexOptions options)
 {
     return(new HexString(@sbyte.GetBytes(), options));
 }
Beispiel #10
0
 public static bool IsEndianBig(this HexOptions options)
 {
     return((options & HexOptions.EndianBig) != HexOptions.None);
 }
Beispiel #11
0
 public static bool IsUpperCase(this HexOptions options)
 {
     return((options & HexOptions.CaseUpper) != HexOptions.None);
 }
Beispiel #12
0
 public static string ToHex(this byte[] bytes, HexOptions options)
 {
     return(new HexString(bytes, options));
 }
Beispiel #13
0
 public static string ToHex(IntPtr p, HexOptions options = DEFAULT)
 => ToHexInternal(p.ToInt64(), options);
Beispiel #14
0
 private static string CreateStringElement(object value, HexOptions options)
 {
     return(options.HasFlagFast(HexOptions.HEX) ? Hex.TryCreateHex(value, options) : value.ToString());
 }
Beispiel #15
0
 public static string ToHex(this ulong? @ulong, ulong defaultValue, HexOptions options)
 {
     return(new HexString((@ulong ?? defaultValue).GetBytes(options), options));
 }
Beispiel #16
0
 /// <summary>
 /// Creates a <see cref="string"/> representation of an <see cref="IEnumerable{T}"/>.
 /// <remarks>
 /// <seealso cref="string.Join{T}"/>
 /// </remarks>
 /// </summary>
 /// <param name="value">Collection</param>
 /// <param name="options"><see cref="HexOptions"/> if the <see cref="IEnumerable{T}"/> is a collection of numbers.</param>
 public static string AutoJoin(this IEnumerable <byte> value, HexOptions options = HexOptions.HEX)
 => CreateString(value, options);
Beispiel #17
0
 public static string ToHex(void *v, HexOptions options = DEFAULT)
 => ToHexInternal((long)v, options);
Beispiel #18
0
 public static string ToHex(ulong l, HexOptions options = DEFAULT)
 => ToHexInternal((long)l, options);
Beispiel #19
0
 private static string CreateString(IEnumerable value, HexOptions options)
 => CreateString(value, StringConstants.JOIN_COMMA, options);
Beispiel #20
0
 public HexString(byte[] source, HexOptions options)
 {
     this.upperCase = options.IsUpperCase();
     this.bytes     = source;
 }
Beispiel #21
0
 public static string TryCreateHex <T>(T value, HexOptions options = DEFAULT)
 {
     return(!TryCreateHex(value, out string str, options) ? value.ToString() : str);
 }
Beispiel #22
0
 /// <summary>
 /// Creates a <see cref="string"/> representation of an <see cref="IEnumerable{T}"/>.
 /// <remarks>
 /// <seealso cref="string.Join{T}"/>
 /// </remarks>
 /// </summary>
 /// <param name="value">Collection</param>
 /// <param name="delim">Delimiter</param>
 /// <param name="options"><see cref="HexOptions"/> if the <see cref="IEnumerable{T}"/> is a collection of numbers.</param>
 public static string AutoJoin(this IEnumerable value, string delim,
                               HexOptions options = HexOptions.NONE)
 => CreateString(value, delim, options);
Beispiel #23
0
 public static string ToHex(this ulong? @ulong, HexOptions options)
 {
     return(new HexString(@ulong.GetBytes(options), options));
 }
Beispiel #24
0
 public static bool HasFlagFast(this HexOptions value, HexOptions flag)
 {
     //return value.HasFlag(flag);
     //if ((testItem & FlagTest.Flag1) == FlagTest.Flag1)
     return((value & flag) == flag);
 }