2929
3030using System ;
3131using System . Collections ;
32- using System . Collections . Immutable ;
3332using System . Collections . Generic ;
33+ using System . Collections . Immutable ;
3434using System . Linq ;
35- using System . Text ;
3635using System . Numerics ;
36+ using System . Text ;
3737
3838namespace HashifyNet . Core . Utilities
3939{
@@ -227,10 +227,14 @@ public decimal AsDecimal()
227227 private decimal AsNumber128_96 ( )
228228 {
229229 if ( BitLength < 1 )
230+ {
230231 throw new ArgumentException ( "Bit Length cannot be smaller than 1." ) ;
232+ }
231233
232234 if ( BitLength > 96 )
235+ {
233236 throw new NotSupportedException ( "Bit Length greater than 96 is not supported." ) ;
237+ }
234238
235239 byte [ ] data = AsByteArray ( ) ;
236240
@@ -246,10 +250,14 @@ private decimal AsNumber128_96()
246250 private long AsNumber64 ( )
247251 {
248252 if ( BitLength < 1 )
253+ {
249254 throw new ArgumentException ( "Bit Length cannot be smaller than 1." ) ;
255+ }
250256
251257 if ( BitLength > 64 )
258+ {
252259 throw new NotSupportedException ( "Bit Length greater than 64 is not supported." ) ;
260+ }
253261
254262 byte [ ] data = AsByteArray ( ) ;
255263
@@ -296,7 +304,7 @@ public DateTime AsDateTime()
296304
297305 if ( BitLength < 64 )
298306 {
299- double maxHashValue = ( double ) ( 1L << BitLength ) ;
307+ double maxHashValue = 1L << BitLength ;
300308 ticks = ( long ) ( ticks / maxHashValue * DateTime . MaxValue . Ticks ) ;
301309 }
302310
@@ -327,7 +335,7 @@ public DateTimeOffset AsDateTimeOffset()
327335
328336 if ( BitLength < 64 )
329337 {
330- double maxHashValue = ( double ) ( 1L << BitLength ) ;
338+ double maxHashValue = 1L << BitLength ;
331339 ticks = ( long ) ( ticks / maxHashValue * DateTimeOffset . MaxValue . Ticks ) ;
332340 }
333341
@@ -358,7 +366,7 @@ public TimeSpan AsTimeSpan()
358366
359367 if ( BitLength < 64 )
360368 {
361- double maxHashValue = ( double ) ( 1L << BitLength ) ;
369+ double maxHashValue = 1L << BitLength ;
362370 ticks = ( long ) ( ticks / maxHashValue * TimeSpan . MaxValue . Ticks ) ;
363371 }
364372
@@ -396,38 +404,22 @@ public string AsBinaryString()
396404 /// <returns><inheritdoc/></returns>
397405 public string AsBase85String ( )
398406 {
399- if ( BitLength < 1 )
400- return "" ;
401-
402- const string Base85Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" ;
403- byte [ ] data = AsByteArray ( ) ;
404- var builder = new StringBuilder ( ) ;
405- int value = 0 ;
406- int count = 0 ;
407+ return AsBase85String ( Base85Variant . Z85 ) ;
408+ }
407409
408- foreach ( byte b in data )
409- {
410- value = ( value << 8 ) | b ;
411- count ++ ;
412- if ( count == 4 )
413- {
414- for ( int i = 4 ; i >= 0 ; i -- )
415- {
416- builder . Append ( Base85Alphabet [ ( value / ( int ) Math . Pow ( 85 , i ) ) % 85 ] ) ;
417- }
418- value = 0 ;
419- count = 0 ;
420- }
421- }
422- if ( count > 0 )
410+ /// <summary>
411+ /// <inheritdoc/>
412+ /// </summary>
413+ /// <param name="variant"><inheritdoc/></param>
414+ /// <returns><inheritdoc/></returns>
415+ public string AsBase85String ( Base85Variant variant )
416+ {
417+ if ( BitLength < 1 )
423418 {
424- value <<= ( 4 - count ) * 8 ; // Pad with zeros
425- for ( int i = 4 ; i >= 5 - count ; i -- )
426- {
427- builder . Append ( Base85Alphabet [ ( value / ( int ) Math . Pow ( 85 , i ) ) % 85 ] ) ;
428- }
419+ return "" ;
429420 }
430- return builder . ToString ( ) ;
421+
422+ return Base85Helper . AsBase85String ( AsByteArray ( ) , variant ) ;
431423 }
432424
433425 /// <summary>
@@ -447,7 +439,9 @@ public string AsBase64String(Base64FormattingOptions formattingOptions = Base64F
447439 public string AsBase58String ( )
448440 {
449441 if ( BitLength < 1 )
442+ {
450443 return "" ;
444+ }
451445
452446 const string Base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ;
453447 byte [ ] data = AsByteArray ( ) ;
@@ -478,7 +472,9 @@ public string AsBase58String()
478472 public string AsBase32String ( )
479473 {
480474 if ( BitLength < 1 )
475+ {
481476 return "" ;
477+ }
482478
483479 const string Base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" ;
484480 byte [ ] data = AsByteArray ( ) ;
0 commit comments