@@ -35,7 +35,7 @@ public static string ToHexString(byte[] inArray, int offset, int length)
35
35
if ( offset > ( inArray . Length - length ) )
36
36
throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
37
37
38
- var builder = new StringBuilder ( ) ;
38
+ var builder = new StringBuilder ( length * 2 ) ;
39
39
40
40
var end = length + offset ;
41
41
for ( int i = offset ; i < end ; i++ )
@@ -65,7 +65,7 @@ public static string ToHexStringLower(byte[] inArray, int offset, int length)
65
65
if ( offset > ( inArray . Length - length ) )
66
66
throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
67
67
68
- var builder = new StringBuilder ( ) ;
68
+ var builder = new StringBuilder ( length * 2 ) ;
69
69
70
70
var end = length + offset ;
71
71
for ( int i = offset ; i < end ; i++ )
@@ -132,15 +132,16 @@ public static bool TryToHexString(ReadOnlySpan<byte> source, Span<char> destinat
132
132
=> Convert . TryToHexString ( source , destination , out charsWritten) ;
133
133
#else
134
134
{
135
- var hexString = ConvertPolyfill . ToHexString ( source ) ;
136
- if ( hexString . TryCopyTo ( destination ) )
135
+ if ( source . Length > destination . Length / 2 )
137
136
{
138
- charsWritten = hexString . Length ;
139
- return true ;
137
+ charsWritten = 0 ;
138
+ return false ;
140
139
}
141
140
142
- charsWritten = 0 ;
143
- return false ;
141
+ var hexString = ConvertPolyfill . ToHexString ( source ) ;
142
+ hexString. CopyTo( destination) ;
143
+ charsWritten = hexString. Length;
144
+ return true;
144
145
}
145
146
#endif
146
147
@@ -153,15 +154,16 @@ public static bool TryToHexStringLower(ReadOnlySpan<byte> source, Span<char> des
153
154
=> Convert . TryToHexStringLower ( source , destination , out charsWritten) ;
154
155
#else
155
156
{
156
- var hexString = ConvertPolyfill . ToHexStringLower ( source ) ;
157
- if ( hexString . TryCopyTo ( destination ) )
157
+ if ( source . Length > destination . Length / 2 )
158
158
{
159
- charsWritten = hexString . Length ;
160
- return true ;
159
+ charsWritten = 0 ;
160
+ return false ;
161
161
}
162
162
163
- charsWritten = 0 ;
164
- return false ;
163
+ var hexString = ConvertPolyfill . ToHexStringLower ( source ) ;
164
+ hexString. CopyTo( destination) ;
165
+ charsWritten = hexString. Length;
166
+ return true;
165
167
}
166
168
#endif
167
169
0 commit comments