@@ -9,22 +9,27 @@ internal static class MiscExtensions
9
9
? value
10
10
: null ;
11
11
12
- public static string ToHexString ( this long l ) => "0x" + l . ToString ( "x" , CultureInfo . InvariantCulture ) ;
12
+ public static string ToHexString ( this long l , bool upperCase = false ) =>
13
+ "0x" + l . ToString ( "x" , CultureInfo . InvariantCulture ) ;
13
14
14
- public static string ToHexString ( this byte [ ] bytes ) => new ReadOnlySpan < byte > ( bytes ) . ToHexString ( ) ;
15
+ public static string ToHexString ( this byte [ ] bytes , bool upperCase = false ) =>
16
+ new ReadOnlySpan < byte > ( bytes ) . ToHexString ( upperCase ) ;
15
17
16
- public static string ToHexString ( this Span < byte > bytes ) => ( ( ReadOnlySpan < byte > ) bytes ) . ToHexString ( ) ;
18
+ public static string ToHexString ( this Span < byte > bytes , bool upperCase = false ) =>
19
+ ( ( ReadOnlySpan < byte > ) bytes ) . ToHexString ( upperCase ) ;
17
20
18
- public static string ToHexString ( this ReadOnlySpan < byte > bytes )
21
+ public static string ToHexString ( this ReadOnlySpan < byte > bytes , bool upperCase = false )
19
22
{
20
23
#if NET5_0_OR_GREATER
21
- return Convert . ToHexString ( bytes ) . ToLowerInvariant ( ) ;
24
+ var s = Convert . ToHexString ( bytes ) ;
25
+ return upperCase ? s : s . ToLowerInvariant ( ) ;
22
26
#else
23
27
var buffer = new StringBuilder ( bytes . Length * 2 ) ;
28
+ var format = upperCase ? "X2" : "x2" ;
24
29
25
30
foreach ( var t in bytes )
26
31
{
27
- buffer . Append ( t . ToString ( "x2" , CultureInfo . InvariantCulture ) ) ;
32
+ buffer . Append ( t . ToString ( format , CultureInfo . InvariantCulture ) ) ;
28
33
}
29
34
30
35
return buffer . ToString ( ) ;
0 commit comments