I have a method that turns a byte array into an integer
public int Encode(string input)
{
var bytes = Encoding.Unicode.GetBytes(input.ToLowerInvariant());
return BitConverter.ToInt64(bytes,0);
}
Why is this integer not different for any input string?
For example
input = "http://www.google.com => 31525695615402088
and
input = "http://www.microsoft.com => 31525695615402088
Because 64 bits is 8 bytes, and so
ToInt64consumes only the first 8 bytes of the input array. What are the first eight bytes of the strings you’ve used?And, as alexm notes,
Encoding.Unicodespecifies UTF-16, in which each character is actually two bytes (usually), so only the first 4 characters count.