c# - 创建短哈希的最佳方法是什么,类似于小 URL 所做的?

标签 c# .net hash short-url

我目前正在使用 MD5 哈希值,但我想找到一些可以创建更短的哈希值的方法,该哈希值仅使用 [a-z][A-Z][0-9]。它只需要大约 5-10 个字符长。

有什么东西已经这样做了吗?

更新 1:

我喜欢 CRC32 哈希。 .NET 中是否有一种干净的计算方法?

更新 2:

我正在使用 Joe 提供的链接中的 CRC32 函数。如何将 uInt 转换为上面定义的字符?

最佳答案

.NET 字符串对象有一个 GetHashCode() 函数。它返回一个整数。 将其转换为十六进制,然后转换为 8 个字符长的字符串。

像这样:

string hashCode = String.Format("{0:X}", sourceString.GetHashCode());

更多信息:http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx

更新:将上述链接中的备注添加到此答案:

The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen is to improve the performance of GetHashCode.

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.

Notes to Callers

The value returned by GetHashCode is platform-dependent. It differs on the 32-bit and 64-bit versions of the .NET Framework.

关于c# - 创建短哈希的最佳方法是什么,类似于小 URL 所做的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1116860/

相关文章:

c# - 将项目添加到缓存而不删除整个集合

c# - 在关闭警告中访问 foreach 变量

c# - WCF WebHttp 服务中的错误处理,仅带有 WebFaultException xml 格式的异常

java - Java 如何计算文件的哈希值?

c# - StackExchange.Redis - 如何将项目添加到 Redis 集合

c# - E pluribus unum : merge objects into one

c# - 通过散列来改进集合比较(过于聪明..?)

ruby-on-rails - 如何获取 Rails fixture 实例内容的哈希值?

c# - System.Web.HttpContext.Current 在请求之间是静态的

.net - Sub Main() 中通过调用自身无限循环?