产生唯一编号来自char * [英] Generate unique no. from char*

查看:85
本文介绍了产生唯一编号来自char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想生成唯一的编号.或char [400]中唯一的键.
因此,请告诉我任何产生唯一编号的技术或算法.或来自char [400]的键
在此先感谢:)

解决方案

一般来讲,没有办法做到,我的意思是char[400]本身是理论上更简单的唯一键(如果您不这样做的话)

如果您需要通用的字符串哈希函数,请在此站点上查看djb2哈希: 解决方案

Genrally speaking there is no way to do that, I mean the char[400] itself is the simpler theoretically possible unique key if you don''t put constraints on your requirements.


If you need a general purpose string hash function then check out the djb2 hashing on this site: http://www.cse.yorku.ca/~oz/hash.html[^]. If I remember right this general purpose string hash function is used in java as well. But in the comments of the previous solution the guys already pointed out: char[400] is already a 400*8 bit number, and you can not generate a unique number represented in less bits for each member of the previous much bigger set. This is where you have to make sure that the hash function you are using doesn''t have a lot of collisions on the set of input data you are usually using. If you don''t exactly know what is the content of your input strings, then the general purpose djb2 hash will be nice for you. But for example if you know that your strings will contain the textual representation of numbers between 0 and 2^32, then a function that converts the string to integer is a perfect hash function (without collisions), but to achieve this we need to know something about the input data (like with compression algorithms). Hashing and compression have a lot in common as a previous poster pointed out. Hashing can improve the performance of some simple data structures like set and map where some operations include searching for a key. Searching is usually done by performing binary halving on a list of sorted items, but with hashing you can achieve much better performance if your hash function doesn''t have a lot of collisions (in english, you don''t have a lot of items with the same hash).


这篇关于产生唯一编号来自char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆