I would like to create unique value generator that would work like this:
I have 8 places for chars and it would create values like this:
00000001
.
0000000z
.
00000010
.
0000001z
etc. so it would create values from 00000001 to zzzzzzzz.
I have only 8 places because this is the size of the field in the database and I can’t change it.
Thanks in advance
,
something like this
public class UniqueKeyMaker
{
private int keys = new int8;
public void Reset()
{
for (int i = 0; i < keys.Length; i++)
keysi = 0;
}
public string NextKey()
{
string key = getCurrentKey();
increment();
return key;
}
private void increment()
{
int i = 7;
while (keysi == 35)
{
keysi = 0;
i--;
}
keysi++;
}
private string getCurrentKey()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 8; i++)
{
if (keysi < 10)
sb.Append((char)(keysi + (int)'0'));
else
sb.Append((char)(keysi - 10 + (int)'a'));
}
return sb.ToString();
}
}
,
Use the bultin random() function, then convert your result to hex once you are done.
,
Are you sure you’re not looking for a GUID?
Same number of chars, different structure: 550e8400-e29b-41d4-a716-446655440000
You can create a new GUID:
System.Guid.NewGuid().ToString(""N"")
Split it every 8 chars and append a dot, like this:
string getUnique()
{
char initial = System.Guid.NewGuid().ToString("N").ToCharArray();
string result="";
for(int i=0; i<initial.Count(); i++){
result=result + initiali;
if((i+1)%4==0 && (i+1)!=initial.Count()){
result = result + ".";
}
}
return result;
}
,
You just want to encode an int into a base 36 system. This might work like like following(probably has a few small mistakes since it’s notepad code):
string IntToID(long id)
{
string result="";
Contract.Require(id>=0);
while(id>0)
{
int digit=id%36;
char digitChar;
if(digit<10)
digitChar='0'+digit;
else
digitChar='a'+(digit-10);
result+=digitChar;
id/=36;
}
result=result.PadLeft('0',8);
return result;
}