14 décembre 2013

HashCode indépendant de la plateforme 32 / 64 bit et du FrameWork

Vous savez que les hashCode du .Net Framework sont dépendant de la plateforme 32 ou 64 bit et de la version du .Net Framework.

Après s'être fait avoir, on cherche une solution :

Celle Proposée sur ce CodeProject me semble intéressante à Tester "Convert String to 64Bit Integer"

Ci dessous un programme de test, les résultats doivent être comparés sur plusieurs machines 32 & 64 bit avec plusieurs versions du framework (3.5 et plus; en dessous ça compile plus)

using System;
using System.Text;

namespace ConsoleApplication1
{
  /// <summary>
  /// Test program
  /// </summary>
  public class Program
  {
    /// <summary>
    /// Programm de tests
    /// </summary>
    /// <param name="args">arguments de la ligne de commande</param>
    public static void Main(string[] args)
    {
      Print("Hello world");
      Print("Hello world2");
      Print("456TY9=1");
      Print(@"Bonjour
je vais bien tout va bien");
      Print("1");
      Print("2");
      Print(string.Empty);
      Print(null);
      Print("123");

      Console.WriteLine();
      Console.WriteLine("-- press a key to quit --");
      Console.ReadKey();
    }

    /// <summary>
    /// Renvoie un code unique en fonction de la chaine fournie
    /// </summary>
    /// <param name="strText">LA chaine à encoder</param>
    /// <returns>Le hash code</returns>
    private static long GetInt64HashCode(string strText)
    {
      long hashCode = 0;
      if (!string.IsNullOrEmpty(strText))
      { // Unicode Encode Covering all characterset
        byte[] byteContents = Encoding.Unicode.GetBytes(strText);
        System.Security.Cryptography.SHA256 hash = new System.Security.Cryptography.SHA256CryptoServiceProvider();
        byte[] hashText = hash.ComputeHash(byteContents);

        // 32Byte hashText separate
        // hashCodeStart = 0~7  8Byte
        // hashCodeMedium = 8~23  8Byte
        // hashCodeEnd = 24~31  8Byte
        // and Fold
        long hashCodeStart = BitConverter.ToInt64(hashText, 0);
        long hashCodeMedium = BitConverter.ToInt64(hashText, 8);
        long hashCodeEnd = BitConverter.ToInt64(hashText, 24);
        hashCode = hashCodeStart ^ hashCodeMedium ^ hashCodeEnd;
      }

      return hashCode;
    }

    /// <summary>
    /// Pour afficher un hash et sons texte
    /// </summary>
    /// <param name="msg">Le texte à afficher</param>
    private static void Print(string msg)
    {
      Console.WriteLine("{0} : {1}", GetInt64HashCode(msg), msg);
    }
  }
}





Résultats des tests sur ma machine Win8.1 32Bits
V4.5

6918273045165231812 : Hello world
8573843817085531220 : Hello world2
1902744953957170490 : 456TY9=1
7665625602362524172 : Bonjour
je vais bien tout va bien
-207971209471392083 : 1
-4968569628414376572 : 2
0 :
0 :
-3845492681683628692 : 123



V4.0

6918273045165231812 : Hello world
8573843817085531220 : Hello world2
1902744953957170490 : 456TY9=1
7665625602362524172 : Bonjour
je vais bien tout va bien
-207971209471392083 : 1
-4968569628414376572 : 2
0 :
0 :
-3845492681683628692 : 123


V3.5

6918273045165231812 : Hello world
8573843817085531220 : Hello world2
1902744953957170490 : 456TY9=1
7665625602362524172 : Bonjour
je vais bien tout va bien
-207971209471392083 : 1
-4968569628414376572 : 2
0 :
0 :
-3845492681683628692 : 123


V3.0
Compile plus !!


Testé sur une machine 64 bits : Mêmes résultats !!
Pour moi cela fonctionne.

05 décembre 2013

Les littéraux C#

EN C# pour écrire un littéral de type decimal il faut ajouter un M majuscule.

Exemple :
decimal v = 3M;


Pour le type double est un D

Exemple :
double d = 5D;


Enjoy