Skip to content

HashCode NN

AlexLEWIS edited this page May 11, 2021 · 4 revisions

The HashCode-NN of Cosmos.Standard is located under the namespace Cosmos.Reflection, and provides the following implementation:

  • HashCode32
  • HashCode64
  • HashCode128
  • HashCode256
  • HashCode512
  • HashCode1024

Basic Value

HashCode-NN internally hosts a set of basic values, and provides a series of methods and function implementations based on this set of basic values.

HashCode Impls Type Numbers of Groups
HashCode32 short 2
HashCode64 uint 2
HashCode128 ulong 2
HashCode256 ulong 4
HashCode512 ulong 8
HashCode1024 ulong 16

Default Value

Each implementation of HashCode-NN provides a default value, and the entry is a static Zero property, such as:

var hc128Val = HashCode128.Zero;

The default value is equal to the 0. Take HashCode128 as an example:

var zeroVal = HashCode128.Zero;
var hc128Val = HashCode128.Parse("00000000000000000000000000000000");

var ret = zeroVal == hc128Val;
// ret is true.

Parse

HashCode-NN provides four methods to facilitate users to parse string values into corresponding HashCode-NN instances:

  • Parse
  • ParseLoosely
  • TryParse
  • TryParseLoosely

These four methods are divided into two types: loose mode and strict mode.

In strict mode:

  • If the length of the string is not enough, return the parsing failure;
  • If the effective length of the given string is not an integer multiple of (the length of the type of the basic value / 4), return the parsing failure;
  • In the parsing process, when analyzing each group of basic values, if the number of looping is insufficient (the length of the type of the basic value / 4) times, return the parsing failure;
  • When the string parsing is completed and the number of number of groups obtained by the parsing is less than the number of groups required by HashCode-NN, return the parsing failure.

In loose mode:

  • If the string length is insufficient, 0 will be used to supplement it.

Take HashCode128 as an example:

var ret = HashCode128.TryParse("123456789abcdef00fedcba987654321", out hash);
// ret is true.

When parsing using Parse or ParseLoosely, FormatException will be thrown if the parsing fails.

Get the value of the specified format

HashCode-NN provides a set of convenient methods to output its internal basic values in a specified format:

  • GetString() - Convert to string in UTF8
  • GetString(Encoding encoding) - Convert to a string with the specified encoding
  • GetHexString() - Output the hexadecimal value, the default is lowercase
  • GetHexString(bool uppercase) - Output hexadecimal value in specified case
  • GetLittleEndianHexString() - Output the hexadecimal value (little endian mode), the default is lowercase
  • GetLittleEndianHexString(bool uppercase) - Output hexadecimal value in specified case (little endian mode)
  • GetBigEndianHexString() - Output hexadecimal value (big endian mode), the default is lower case
  • GetBigEndianHexString(bool uppercase) - Output the hexadecimal value in the specified case (big endian mode)
  • GetBinString() - Output binary value
  • GetBinString(bool complementZero) - Output the binary value and complete 0
  • GetBase64String() - Output Base64 string
  • GetByteArray() - Output byte[] value
  • GetBitArray() - Output BitArray value

Backward conversion

HashCode-NN allows backward conversion, for example, HashCode128 can be converted to 2 HashCode64s. The following is a detailed table:

HashCode Impls Target Impls Count of target impls
HashCode32 None 0
hashCode64 HashCode32 2
hashCode128 HashCode64 2
hashCode256 HashCode64
HashCode128
4
2
hashCode512 HashCode64
HashCode128
HashCode256
8
4
2
hashCode1024 HashCode64
HashCode128
HashCode256
HashCode512
16
8
4
2

Clone this wiki locally