cesarz
cesarz
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Well, anyways, thanks for the help!
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
and idk, maybe an additional check in the loop if there are any correct characters left? There's a possibility that someone will for some reason name a file "Abc######" so the loop would continue for 6 more times for no reason, but idk if it makes too much of a difference
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
It skips the incorrect characters (-), adds the first 6 to the ID, if 6 are already added then it skips through all the other correct chars until there are only 7 left
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
int length = chars.Length;
int correctChars = 0;
for (int i = 0; i < length; ++i)
{
switch (chars[i])
{
case >= '0' and <= '9':
++correctChars;
chars[i] -= '0';
break;
case >= 'A' and <= 'Z':
++correctChars;
chars[i] -= 'A';
break;
case >= 'a' and <= 'z':
++correctChars;
chars[i] -= 'a';
break;
default:
chars[i] = '-';
break;
}
}
ulong ID = 0;
for (int i = 0, x = 0; i < length; ++i)
{
if (chars[i] == '-')
continue;
if (x != 6)
{
ID += chars[i] * (ulong)Mathf.Pow(26, x++);
--correctChars;
}
else
{
if (correctChars > 7)
{
--correctChars;
continue;
}
++x;
}
}
return ID;
}
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
int length = chars.Length;
int correctChars = 0;
for (int i = 0; i < length; ++i)
{
switch (chars[i])
{
case >= '0' and <= '9':
++correctChars;
chars[i] -= '0';
break;
case >= 'A' and <= 'Z':
++correctChars;
chars[i] -= 'A';
break;
case >= 'a' and <= 'z':
++correctChars;
chars[i] -= 'a';
break;
default:
chars[i] = '-';
break;
}
}
ulong ID = 0;
for (int i = 0, x = 0; i < length; ++i)
{
if (chars[i] == '-')
continue;
if (x != 6)
{
ID += chars[i] * (ulong)Mathf.Pow(26, x++);
--correctChars;
}
else
{
if (correctChars > 7)
{
--correctChars;
continue;
}
++x;
}
}
return ID;
}
How about this one? Now there are no queues, only the initial char array
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
@#$_&-+()/ etc
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
and some are ignored
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
first 6 and last 7
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
-1 would be placed during the first loop with the switch in places of characters which aren't A-Z/a-z/0-9 alongside with an incrementing length variable for the ones which are
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
What would you recommend as an alternative? Would an array, where -1 is to be skipped, be a better alternative?
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
Queue<int> inputQueue = new Queue<int>();
Queue<int> idGenQueue = new Queue<int>();
foreach (char c in chars)
{
switch (c)
{
case >= '0' and <= '9':
inputQueue.Enqueue(c - '0');
break;
case >= 'A' and <= 'Z':
inputQueue.Enqueue(c - 'A');
break;
case >= 'a' and <= 'z':
inputQueue.Enqueue(c - 'a');
break;
}
}
int length = inputQueue.Count;
if (length < 13)
{
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
else
{
for (int i = 0; i < 6; ++i)
idGenQueue.Enqueue(inputQueue.Dequeue());
while (inputQueue.Count > 7)
inputQueue.Dequeue();
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
ulong ID = 0;
length = idGenQueue.Count;
for (int i = 0; i < length; ++i)
ID += (ulong)idGenQueue.Dequeue() * (ulong)Mathf.Pow(26, i);
return ID;
}
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
Queue<int> inputQueue = new Queue<int>();
Queue<int> idGenQueue = new Queue<int>();
foreach (char c in chars)
{
switch (c)
{
case >= '0' and <= '9':
inputQueue.Enqueue(c - '0');
break;
case >= 'A' and <= 'Z':
inputQueue.Enqueue(c - 'A');
break;
case >= 'a' and <= 'z':
inputQueue.Enqueue(c - 'a');
break;
}
}
int length = inputQueue.Count;
if (length < 13)
{
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
else
{
for (int i = 0; i < 6; ++i)
idGenQueue.Enqueue(inputQueue.Dequeue());
while (inputQueue.Count > 7)
inputQueue.Dequeue();
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
ulong ID = 0;
length = idGenQueue.Count;
for (int i = 0; i < length; ++i)
ID += (ulong)idGenQueue.Dequeue() * (ulong)Mathf.Pow(26, i);
return ID;
}
Here's the final implementation. If anyone has any ideas on how to improve this further then feel free to ping me
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Should probably work fine as long as nobody uses names such as "hhhhhhhhhh" and "hhhhhhhhhhhhhhh" which will then probably generate the same ID
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
13 characters is quite a lot to work with already. I can divide it in 'half' => 6 and 7 characters Take 6 characters from the beginning of the input And 7 characters from the end of the input Unless the input length is < 13, then I just take every character. That would probably work out fine enough?
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Now there's a 13 character limit... Any progress is good.
26 replies