In the past, I created a function that generates a unique identifier (number) from a string. Today I find that this is not as straightforward as it should be. Never seen a problem with this. Today, two different inputs generate the same identifier (number).
I use the same technique in Delphi, C ++, PHP and Javascript to generate the same identifier, so it makes no difference when different languages ββare involved in the project. For example, it can be convenient for communication, for HTML id, tempfiles, etc.
In general, what I am doing is calculating CRC16 rows, adding the sum and returning it.
For example, these two lines generate the same identifier (number):
o.uniqueId( 'M:/Mijn Muziek/Various Artists/Revs & ElBee - Tell It To My Heart.mp3' ); o.uniqueId( 'M:/Mijn Muziek/Various Artists/Dwight Yoakam - The Back Of Your Hand.Mp3');
Both of them generate identifier 224904.
The following example is a javascript example. My question is: how can I avoid (with a little change) that it creates a duplicate? (In case you are interested in what βo.β Means, This is the object these functions relate to):
o.getCrc16 = function(s, bSumPos) { if(typeof s !== 'string' || s.length === 0) { return 0; } var crc = 0xFFFF, L = s.length, sum = 0, x = 0, j = 0; for(var i = 0; i < L; i++) { j = s.charCodeAt(i); sum += ((i + 1) * j); x = ((crc >> 8) ^ j) & 0xFF; x ^= x >> 4; crc = ((crc << 8) ^ (x << 12) ^ (x << 5) ^ x) & 0xFFFF; } return crc + ((bSumPos ? 1 : 0) * sum); } o.uniqueId = function(s, bres) { if(s == undefined || typeof s != 'string') { if(!o.___uqidc) { o.___uqidc = 0; } else { ++o.___uqidc; } var od = new Date(), i = s = od.getTime() + '' + o.___uqidc; } else { var i = o.getCrc16(s, true); } return((bres) ? 'res:' : '') + (i + (i ? s.length : 0)); };
How to avoid duplicates using a small code change?