10
10
// modifications are permitted.
11
11
12
12
using System ;
13
+ using System . Runtime . CompilerServices ;
13
14
using System . Text ;
14
15
15
16
namespace Neo . Extensions
16
17
{
17
18
public static class ByteExtensions
18
19
{
20
+ private const string s_hexChars = "0123456789abcdef" ;
21
+
19
22
/// <summary>
20
23
/// Converts a byte array to hex <see cref="string"/>.
21
24
/// </summary>
22
25
/// <param name="value">The byte array to convert.</param>
23
26
/// <returns>The converted hex <see cref="string"/>.</returns>
27
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
24
28
public static string ToHexString ( this byte [ ] value )
25
29
{
26
- StringBuilder sb = new ( ) ;
27
- foreach ( var b in value )
28
- sb . AppendFormat ( "{0:x2}" , b ) ;
29
- return sb . ToString ( ) ;
30
+ #if NET9_0_OR_GREATER
31
+ return Convert . ToHexStringLower ( value ) ;
32
+ #else
33
+ return string . Create ( value . Length * 2 , value , ( span , bytes ) =>
34
+ {
35
+ for ( var i = 0 ; i < bytes . Length ; i ++ )
36
+ {
37
+ var b = bytes [ i ] ;
38
+ span [ i * 2 ] = s_hexChars [ b >> 4 ] ;
39
+ span [ i * 2 + 1 ] = s_hexChars [ b & 0xF ] ;
40
+ }
41
+ } ) ;
42
+ #endif
30
43
}
31
44
32
45
/// <summary>
@@ -35,25 +48,44 @@ public static string ToHexString(this byte[] value)
35
48
/// <param name="value">The byte array to convert.</param>
36
49
/// <param name="reverse">Indicates whether it should be converted in the reversed byte order.</param>
37
50
/// <returns>The converted hex <see cref="string"/>.</returns>
51
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
38
52
public static string ToHexString ( this byte [ ] value , bool reverse = false )
39
53
{
40
- StringBuilder sb = new ( ) ;
41
- for ( var i = 0 ; i < value . Length ; i ++ )
42
- sb . AppendFormat ( "{0:x2}" , value [ reverse ? value . Length - i - 1 : i ] ) ;
43
- return sb . ToString ( ) ;
54
+ if ( ! reverse )
55
+ return ToHexString ( value ) ;
56
+
57
+ return string . Create ( value . Length * 2 , value , ( span , bytes ) =>
58
+ {
59
+ for ( var i = 0 ; i < bytes . Length ; i ++ )
60
+ {
61
+ var b = bytes [ bytes . Length - i - 1 ] ;
62
+ span [ i * 2 ] = s_hexChars [ b >> 4 ] ;
63
+ span [ i * 2 + 1 ] = s_hexChars [ b & 0xF ] ;
64
+ }
65
+ } ) ;
44
66
}
45
67
46
68
/// <summary>
47
69
/// Converts a byte array to hex <see cref="string"/>.
48
70
/// </summary>
49
71
/// <param name="value">The byte array to convert.</param>
50
72
/// <returns>The converted hex <see cref="string"/>.</returns>
73
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
51
74
public static string ToHexString ( this ReadOnlySpan < byte > value )
52
75
{
53
- StringBuilder sb = new ( ) ;
54
- foreach ( var b in value )
55
- sb . AppendFormat ( "{0:x2}" , b ) ;
76
+ #if NET9_0_OR_GREATER
77
+ return Convert . ToHexStringLower ( value ) ;
78
+ #else
79
+ // string.Create with ReadOnlySpan<char> not supported in NET5 or lower
80
+ var sb = new StringBuilder ( value . Length * 2 ) ;
81
+ for ( var i = 0 ; i < value . Length ; i ++ )
82
+ {
83
+ var b = value [ i ] ;
84
+ sb . Append ( s_hexChars [ b >> 4 ] ) ;
85
+ sb . Append ( s_hexChars [ b & 0xF ] ) ;
86
+ }
56
87
return sb . ToString ( ) ;
88
+ #endif
57
89
}
58
90
}
59
91
}
0 commit comments