public static byte[] GetBytes(this ulong @ulong, HexOptions options) { byte[] bytes = BitConverter.GetBytes(@ulong); if (options.IsEndianBig()) { Array.Reverse(bytes); } return(bytes); }
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); }
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)); } }
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); }
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)); }
// 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()); }
public static string ToHex(this uint? @uint, HexOptions options) { return(new HexString(@uint.GetBytes(options), options)); }
public static string ToHex(this ushort @ushort, HexOptions options) { return(new HexString(@ushort.GetBytes(options), options)); }
public static string ToHex(this sbyte? @sbyte, HexOptions options) { return(new HexString(@sbyte.GetBytes(), options)); }
public static bool IsEndianBig(this HexOptions options) { return((options & HexOptions.EndianBig) != HexOptions.None); }
public static bool IsUpperCase(this HexOptions options) { return((options & HexOptions.CaseUpper) != HexOptions.None); }
public static string ToHex(this byte[] bytes, HexOptions options) { return(new HexString(bytes, options)); }
public static string ToHex(IntPtr p, HexOptions options = DEFAULT) => ToHexInternal(p.ToInt64(), options);
private static string CreateStringElement(object value, HexOptions options) { return(options.HasFlagFast(HexOptions.HEX) ? Hex.TryCreateHex(value, options) : value.ToString()); }
public static string ToHex(this ulong? @ulong, ulong defaultValue, HexOptions options) { return(new HexString((@ulong ?? defaultValue).GetBytes(options), options)); }
/// <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);
public static string ToHex(void *v, HexOptions options = DEFAULT) => ToHexInternal((long)v, options);
public static string ToHex(ulong l, HexOptions options = DEFAULT) => ToHexInternal((long)l, options);
private static string CreateString(IEnumerable value, HexOptions options) => CreateString(value, StringConstants.JOIN_COMMA, options);
public HexString(byte[] source, HexOptions options) { this.upperCase = options.IsUpperCase(); this.bytes = source; }
public static string TryCreateHex <T>(T value, HexOptions options = DEFAULT) { return(!TryCreateHex(value, out string str, options) ? value.ToString() : str); }
/// <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);
public static string ToHex(this ulong? @ulong, HexOptions options) { return(new HexString(@ulong.GetBytes(options), options)); }
public static bool HasFlagFast(this HexOptions value, HexOptions flag) { //return value.HasFlag(flag); //if ((testItem & FlagTest.Flag1) == FlagTest.Flag1) return((value & flag) == flag); }