-2

I have a problem in C# with List type. I can't access to List variable elements with decimal value. I have below function. That get position value and a string_list (exactly list of various characters) and find that specific string in that position in string_list.

 private string generator(decimal position, List<string> string_list) 
        {
            List<decimal> mod_list = new List<decimal>();
            string output = "";

            while (position >= 0)
            {
                mod_list.Add(position % string_list.Count);
                position -= position % string_list.Count;
                position /= (decimal)string_list.Count;
                position -= 1;
            }
            int z = 1;
            for (decimal i = (mod_list.Count - 1); i >= 0; i += -1)
                output += string_list[mod_list[i]];

            return output;
        }

in line output += string_list[mod_list[i]]; Visual Studio gets an error from me because the index value should be "int". I test this function with "int" data type and get result but i want it with decimal. Error message in Visual Studio: Argument 1: cannot convert from 'decimal' to 'int' Thanx for your answers ^_^

ali desu
  • 11
  • 4
  • 1
    The index needs to be an integer value (wouldn't really make sense to have a decimal index). mod_list is a list of decimals. Therefore, mod_list[i] is a decimal, and string_list[mod_list[i]] is an attempt to retrieve an item from string_list using a decimal index. – Kei Dec 20 '19 at 06:53
  • 1
    Not sure what you are trying to do, but if you **really** need mod_list to be a List, a workaround would be to do a cast. `string_list[(int)mod_list[i]]` Note, however, that this would truncate any decimal places. – Kei Dec 20 '19 at 07:00
  • @Kei, i know that. let's think user want to continue process until several hour, maybe the int value not be enoght. What should we do ? – ali desu Dec 20 '19 at 07:30
  • @alidesu int values can be encoded on 8/16/32 bits so don't worry for that – Arlien Dec 20 '19 at 07:33
  • For your second comment, ```string_list[(int)mod_list[i]]```, didn't worked. – ali desu Dec 20 '19 at 07:34
  • @arlien, Can you guarantee that this is enough? The program will not be crashed after several hours ? – ali desu Dec 20 '19 at 07:35
  • @alidesu not `string_list[(int)mod_list[i]]` but `string_list[mod_list[(int)i]]` – Arlien Dec 20 '19 at 07:36
  • @alidesu You need to understand that you don't have to plan for every possibility. You need to understand what CAN really append what cannot. So you need to rethink your code and maybe tell that if the user does it 1000x times, the code should stop (idk for here idk what it's doing) – Arlien Dec 20 '19 at 07:37
  • i have char list with 92 characters. i want to generate all 92^92 situation and obviously this is a lot. I think the ```int``` can't be enough. If i have mistake please tell me. – ali desu Dec 20 '19 at 07:38
  • @Arlien exactly this function used for bruteforce attack to crack hash. therefore i this ```int``` value cannot be enough. – ali desu Dec 20 '19 at 07:48
  • My apologies, I didn't realize that your iterator (i) was also a decimal. You'll also need to change `for (decimal i = `... to `for (int i = `. List.Count is an int anyway so given your loop conditions, there would be no situation where `i` would be anything but an int. Once you make that change `string_list[(int)mod_list[i]]` should work. – Kei Dec 20 '19 at 07:49
  • @alidesu can you try the code ive given you in my answer please – Arlien Dec 20 '19 at 07:51
  • @Kei No problem. you're right. Thanx for your help. – ali desu Dec 20 '19 at 07:52
  • @Arlien i tried ```string_list[mod_list[(int)i]]```, but didn't worked. – ali desu Dec 20 '19 at 07:53
  • No i mean in the answer below – Arlien Dec 20 '19 at 07:54
  • 1
    Dear @Arlien, Also thank you. i owe you one ^_^ – ali desu Dec 20 '19 at 07:54
  • @Arlien, Yes Of course. I Can tell the user this process can be take a while. Please stop the process. – ali desu Dec 20 '19 at 07:55
  • Yes you can do that ++ – Arlien Dec 20 '19 at 07:57
  • Just to note though, 92 raised to the power of 92 is 4.6e180. Just to put that in perspective, some estimate the number of atoms in the observable universe to be about 1e80. Also note that according to [this answer](https://stackoverflow.com/a/2009246/11981207), Lists are limited to 2GB and wouldn't be able to contain it all. – Kei Dec 20 '19 at 08:05
  • @Kei I didn't generate all strings in one time. i put the function in a ```while``` loop and generate and specific by passing the value as ```position``` to function to calculate the string in that position of character list. My character list contain: 0-9, a-z, A-Z and all special characters. i nevel calculate all of them in one time. just calculate one string in specific position. – ali desu Dec 20 '19 at 08:16

1 Answers1

0

Simply change the decimal type to int, he index type is int, it's said in the error message.

private string generator(int position, List<string> string_list) {
    List<int> mod_list = new List<int> ();
    string output = "";

    while (position >= 0) {
        mod_list.Add(position % string_list.Count);
        position -= position % string_list.Count;
        position /= string_list.Count;
        position -= 1;
    }
    int z = 1;
    for (int i = (mod_list.Count - 1); i >= 0; i += -1)
        output += string_list[mod_list[i]];

    return output;
}
Arlien
  • 132
  • 1
  • 17
  • VS gets an error from me from line: ```position /= (decimal) string_list.Count;``` It's OK, i define message box and tell the user to stop process because it will take a while. Thank you any way :) – ali desu Dec 20 '19 at 08:00
  • Yeah. I edited the code myself before you edit. it's done. – ali desu Dec 20 '19 at 08:06
  • I really thank you. You helped me a lot. Have a good time :D And i forgot to remove ```int z = 1;``` xD This is extra – ali desu Dec 20 '19 at 08:07