将字符串编码为整数 .NET
我有一个字符串,我想将其唯一地表示为整数。
例如: A3FJEI = 34950140
我将如何编写 EncodeAsInteger(string) 方法。我知道字符串中的字符数量会使整数大大增加,迫使该值变成long,而不是int。
由于我需要该值是整数,因此我不需要数字表示对于字符串来说是完全唯一的。
也许我可以遍历字符串的所有字符并对字符的数字键码求和。
I have a string that I would like represented uniquely as an integer.
For example: A3FJEI = 34950140
How would I go about writing a EncodeAsInteger(string) method. I understand that the amount of characters in the string will make the integer increase greatly, forcing the value to become a long, not an int.
Since I need the value to be an integer, I don't need the numerical representation to be entirely unique to the string.
Maybe I can foreach through all the characters of the string and sum the numerical keycode of the character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这些是您的要求:
那么你很幸运,因为这正是
GetHashCode()
方法可以! MSDN 页面提供了一些示例哈希代码。请注意,这些是完全任意的,并且可能在 CLR 的未来版本中更改。If these are your requirements:
Then you're in luck, because this is exactly what the
GetHashCode()
method does! The MSDN page gives some sample hash codes. Note that these are completely arbitrary and can change in future versions of the CLR.