1

I have a string made up of 2 parts (see code)

I want to know the UnknownDeterministicFunction which returns a Long, which can deterministically produce the same long for a given string.

private void MyProgram()
{
    string resultStr = "XXX"+"12345678";
    //1st part is a string of characters (the "XXX")
    //2nd part is a string of numbers (the "12345678")

    long resultLng = UnknownDeterministicFunction(myString);
}

private long UnknownDeterministicFunction(string inputStr)
{
    // ???
}

Is this possible in C#?

MajorInc
  • 342
  • 3
  • 12

2 Answers2

3

First of all

  • there are 8 ** (2 ** 30) different strings (which up to 2 GB long)
  • there are 2 ** 64 differrent long (which are 64-bit integers)

So you can't guarantee long be unique (good old Pigeonhole principle). If you are ready for possible, though improbable collisions (i.e. different strings can well return the same long) you may want to implement hash functions, e.g.

hash function for string

or

Good Hash Function for Strings

usually, hash function returns Int32; in that case just combine two int into one long

int hash1 = GetHashOneAlgorithm(myString); 
int hash2 = GetHashAnotherAlgorithm(myString); 

long result = ((long) hash1 << 32) | hash2;
Community
  • 1
  • 1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
-1

OK the answer is simple.

private long UnknownDeterministicFunction(string inputStr)
{
    //not taking care of null...
    return (long)inputStr.GetHashCode()
}
MajorInc
  • 342
  • 3
  • 12
  • 1
    Two problems: first: if `inputStr == null` you'll have exception thrown; second: *hash* is good choice, but it doesn't *unique*. Sorry, the answer is not *that simple*. – Dmitry Bychenko Jul 15 '16 at 13:41
  • Counter example: *different* strings `"9331582"` and `"942"` returns the *same* hash `-1864841629` (*C# 6.0 .Net 4.6*); so your solution didn't produce *unique* `long` – Dmitry Bychenko Jul 15 '16 at 13:48