1+ // <auto-generated />
2+
3+ #pragma warning disable
4+
5+ namespace Polyfills ;
6+
7+ using System ;
8+ using System . Text ;
9+ using System . ComponentModel ;
10+ using System . Collections . Generic ;
11+ using System . Diagnostics ;
12+ using System . Diagnostics . CodeAnalysis ;
13+
14+ [ ExcludeFromCodeCoverage ]
15+ [ DebuggerNonUserCode ]
16+ #if PolyPublic
17+ public
18+ #endif
19+ static partial class ConvertPolyfill
20+ {
21+ /// <summary>
22+ /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters.
23+ /// Parameters specify the subset as an offset in the input array and the number of elements in the array to convert.
24+ /// </summary>
25+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()-system-int32-system-int32)
26+ public static string ToHexString ( byte [ ] inArray , int offset , int length )
27+ #if NET5_0_OR_GREATER
28+ => Convert . ToHexString ( inArray , offset , length ) ;
29+ #else
30+ {
31+ if ( length < 0 )
32+ throw new ArgumentOutOfRangeException ( nameof ( length ) ) ;
33+ if ( offset < 0 )
34+ throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
35+ if ( offset > ( inArray . Length - length ) )
36+ throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
37+
38+ var builder = new StringBuilder ( ) ;
39+
40+ var end = length + offset ;
41+ for ( int i = offset ; i < end ; i++ )
42+ {
43+ var item = inArray [ i ] ;
44+ builder . Append ( item . ToString ( "X2" ) ) ;
45+ }
46+
47+ return builder . ToString ( ) ;
48+ }
49+ #endif
50+
51+ /// <summary>
52+ /// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters.
53+ /// Parameters specify the subset as an offset in the input array and the number of elements in the array to convert.
54+ /// </summary>
55+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()-system-int32-system-int32)
56+ public static string ToHexStringLower ( byte [ ] inArray , int offset , int length )
57+ #if NET9_0_OR_GREATER
58+ => Convert . ToHexStringLower ( inArray , offset , length ) ;
59+ #else
60+ {
61+ if ( length < 0 )
62+ throw new ArgumentOutOfRangeException ( nameof ( length ) ) ;
63+ if ( offset < 0 )
64+ throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
65+ if ( offset > ( inArray . Length - length ) )
66+ throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
67+
68+ var builder = new StringBuilder ( ) ;
69+
70+ var end = length + offset ;
71+ for ( int i = offset ; i < end ; i++ )
72+ {
73+ var item = inArray [ i ] ;
74+ builder . Append ( item . ToString ( "x2" ) ) ;
75+ }
76+
77+ return builder . ToString ( ) ;
78+ }
79+ #endif
80+
81+ /// <summary>
82+ /// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters.
83+ /// </summary>
84+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte())
85+ public static string ToHexString ( byte [ ] inArray ) =>
86+ #if NET5_0_OR_GREATER
87+ Convert . ToHexString ( inArray ) ;
88+ #else
89+ ConvertPolyfill . ToHexString ( inArray , 0 , inArray . Length ) ;
90+ #endif
91+
92+ /// <summary>
93+ /// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters.
94+ /// </summary>
95+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte())
96+ public static string ToHexStringLower ( byte [ ] inArray ) =>
97+ #if NET9_0_OR_GREATER
98+ Convert . ToHexStringLower ( inArray ) ;
99+ #else
100+ ConvertPolyfill . ToHexStringLower ( inArray , 0 , inArray . Length ) ;
101+ #endif
102+
103+ #if FeatureMemory
104+ /// <summary>
105+ /// Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters.
106+ /// </summary>
107+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-readonlyspan((system-byte)))
108+ public static string ToHexString ( ReadOnlySpan < byte > bytes ) =>
109+ #if NET5_0_OR_GREATER
110+ Convert . ToHexString ( bytes ) ;
111+ #else
112+ ConvertPolyfill . ToHexString ( bytes . ToArray ( ) ) ;
113+ #endif
114+
115+ /// <summary>
116+ /// Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters.
117+ /// </summary>
118+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-readonlyspan((system-byte)))
119+ public static string ToHexStringLower ( ReadOnlySpan < byte > bytes ) =>
120+ #if NET9_0_OR_GREATER
121+ Convert . ToHexStringLower ( bytes ) ;
122+ #else
123+ ConvertPolyfill . ToHexStringLower ( bytes . ToArray ( ) ) ;
124+ #endif
125+
126+ /// <summary>
127+ /// Converts a span of 8-bit unsigned integers to its equivalent span representation that is encoded with uppercase hex characters.
128+ /// </summary>
129+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstring
130+ public static bool TryToHexString ( ReadOnlySpan < byte > source , Span < char > destination , out int charsWritten )
131+ #if NET9_0_OR_GREATER
132+ => Convert . TryToHexString ( source , destination , out charsWritten) ;
133+ #else
134+ {
135+ var hexString = ConvertPolyfill . ToHexString ( source ) ;
136+ if ( hexString . TryCopyTo ( destination ) )
137+ {
138+ charsWritten = hexString . Length ;
139+ return true ;
140+ }
141+
142+ charsWritten = 0 ;
143+ return false ;
144+ }
145+ #endif
146+
147+ /// <summary>
148+ /// Converts a span of 8-bit unsigned integers to its equivalent span representation that is encoded with lowercase hex characters.
149+ /// </summary>
150+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstringlower
151+ public static bool TryToHexStringLower ( ReadOnlySpan < byte > source , Span < char > destination , out int charsWritten )
152+ #if NET9_0_OR_GREATER
153+ => Convert . TryToHexStringLower ( source , destination , out charsWritten) ;
154+ #else
155+ {
156+ var hexString = ConvertPolyfill . ToHexStringLower ( source ) ;
157+ if ( hexString . TryCopyTo ( destination ) )
158+ {
159+ charsWritten = hexString . Length ;
160+ return true ;
161+ }
162+
163+ charsWritten = 0 ;
164+ return false ;
165+ }
166+ #endif
167+
168+
169+ #endif
170+
171+ /// <summary>
172+ /// Converts the specified string, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array.
173+ /// </summary>
174+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-string)
175+ public static byte [ ] FromHexString ( string hexString )
176+ #if NET5_0_OR_GREATER
177+ => Convert . FromHexString ( hexString ) ;
178+ #else
179+ {
180+ if ( hexString . Length % 2 != 0 )
181+ throw new FormatException ( "Hex string must have an even length." ) ;
182+
183+ var result = new byte [ hexString . Length / 2 ] ;
184+
185+ for ( int i = 0 ; i < result . Length ; i ++ )
186+ {
187+ result [ i ] = ( byte ) ( ( GetHexValue ( hexString [ i * 2 ] ) << 4 ) + GetHexValue ( hexString [ i * 2 + 1 ] ) ) ;
188+ }
189+
190+ return result ;
191+
192+ static int GetHexValue ( char hex )
193+ {
194+ return hex switch
195+ {
196+ >= '0' and <= '9' => hex - '0' ,
197+ >= 'A' and <= 'F' => hex - 'A' + 10 ,
198+ >= 'a' and <= 'f' => hex - 'a' + 10 ,
199+ _ => throw new FormatException ( $ "Invalid hex character: { hex } ")
200+ } ;
201+ }
202+ }
203+ #endif
204+
205+ #if FeatureMemory
206+ /// <summary>
207+ /// Converts the span, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array.
208+ /// </summary>
209+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-readonlyspan((system-char)))
210+ public static byte [ ] FromHexString ( ReadOnlySpan < char > chars )
211+ #if NET5_0_OR_GREATER
212+ => Convert . FromHexString ( chars ) ;
213+ #else
214+ => ConvertPolyfill . FromHexString ( chars . ToString ( ) ) ;
215+ #endif
216+ #endif
217+ }
0 commit comments