Click here to Skip to main content
14,457,171 members

Convert String to 64bit Integer

Rate this:
4.16 (11 votes)
Please Sign up or sign in to vote.
4.16 (11 votes)
20 Mar 2009CPOL
A C# library general purpose function converts string to unique 64bit integer like object.GetHashCode()


.NET Framework provides the 'GetHashcode()' function returning a 32bit Integer. You can convert 'string' to 32bit Integer but it doesn't guarantee a unique number. Here is the function that converts string to a unique 64bit integer, avoids collision domain level string store, compare and trace.


If you need to store a large amount of URLs in your database, you must define 'character' index for manipulation. If you can generate a matching 'one-string-to-one-numeric-value' that can use as a numeric 'Key' instead of a variant length of string.

Using the Code

  1. Convert a variable length of string to fixed length of hashcode, and it must have fast hashing speed, so use .NET provided System.Security.Cryptography.SHA256CryptoServiceProvider.
  2. Convert 32 byte hashcode to 8 byte integer, avoiding making a collision.
/// <summary>
/// Return unique Int64 value for input string
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
static Int64 GetInt64HashCode(string strText)
    Int64 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
        Int64 hashCodeStart = BitConverter.ToInt64(hashText, 0);
        Int64 hashCodeMedium = BitConverter.ToInt64(hashText, 8);
        Int64 hashCodeEnd = BitConverter.ToInt64(hashText, 24);
        hashCode = hashCodeStart ^ hashCodeMedium ^ hashCodeEnd;
    return (hashCode);

Collision and Performance Test

Tested platform: Core2Duo, Windows 2003 Server SP2, .NET Framework 3.5 SP1

10,000,000 Times generate GetInt64HashCode

Collision: Not found

100,000 Times generate ElapsedTime: 830 milliseconds

10,000,000 Times generate .NET Framework provided object.GetHashCode

Collision: 4,150 found

100,000 Times generate ElapsedTime: 35 milliseconds

Points of Interest

I know that Cryptography.SHA256 does not provide perfect collision avoided hash value and compressed 32Byte to 8Byte can increase collision probability, but I think the above function shows enough performance and avoids collision.

Your reply will make the function more efficient and reliable.

This function now uses and collects large amount of URLs. There is no collision for 40,000,000 unique URLs.


  • 20th March, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Seoul , Korea
Korea (Republic of) Korea (Republic of)
develop, management software, website last six years, on .netframework environment

Interested in Architect and Framework

you can visit my blog :

articles for c#, framework, architect, and recommend books
but, only korean languages.
in the immediate future I will posts english version also.

Comments and Discussions

QuestionPerhaps, for this purpose, you might also to use simply GetHashCode. Pin
stefan.babos21-Jun-18 22:36
Memberstefan.babos21-Jun-18 22:36 
QuestionImprovement Pin
oleum17-Jul-17 5:47
Memberoleum17-Jul-17 5:47 
QuestionJavascript/Typescript/Angular Pin
jadeboy12-Feb-17 15:07
Memberjadeboy12-Feb-17 15:07 
GeneralMy vote of 5 Pin
Rob 1234523-Oct-12 14:35
MemberRob 1234523-Oct-12 14:35 
GeneralCRC Pin
AndreyMir28-Mar-09 19:55
MemberAndreyMir28-Mar-09 19:55 
QuestionRe: CRC Pin
Composition412-Apr-09 18:42
MemberComposition412-Apr-09 18:42 
GeneralAlternative approach Pin
djlove26-Mar-09 12:39
Memberdjlove26-Mar-09 12:39 
AnswerRe: Alternative approach Pin
Composition426-Mar-09 17:41
MemberComposition426-Mar-09 17:41 
Generalmore tested 100,000,000 unique string for hashe value , collision not found yet Pin
Composition422-Mar-09 14:56
MemberComposition422-Mar-09 14:56 
General"unique 64bit inteager" Pin
harold aptroot21-Mar-09 14:08
Memberharold aptroot21-Mar-09 14:08 
GeneralRe: "unique 64bit inteager" Pin
assmax22-Mar-09 0:04
Memberassmax22-Mar-09 0:04 
AnswerRe: "unique 64bit inteager" Pin
Composition422-Mar-09 5:43
MemberComposition422-Mar-09 5:43 
AnswerRe: "unique 64bit inteager" Pin
Composition422-Mar-09 5:56
MemberComposition422-Mar-09 5:56 
GeneralRe: "unique 64bit inteager" Pin
harold aptroot22-Mar-09 6:56
Memberharold aptroot22-Mar-09 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 20 Mar 2009


21 bookmarked