Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign integer value to letter

Tags:

c#

So i have this problem. I have Text Box with text containing 12 numbers. So for example 012345678912. Now i don't know how to assign the first, then the second .... and so on numbers to letters like a,b,c,d,e,f,g,h,i,j,k,l because i need them to calculate the 13th (m) number with the following formula

m = 11 − (( 7*(a+g) + 6*(b+h) + 5*(c+i) + 4*(d+j) + 3*(e+k) + 2*(f+l) ) mod 11)

Also i tried to make this formula in to c# readable text and i got this, hope it is correct

int result = (int)new DataTable().Compute(" 11 − (( 7*(a+g) + 6*(b+h) + 5*(c+i) + 4*(d+j) + 3*(e+k) + 2*(f+l) ) mod 11)", null);
like image 510
Сашко Мицевски Avatar asked Apr 29 '26 01:04

Сашко Мицевски


1 Answers

there are plenty of approaches that you can adapt to achieve this. But if you really want to declare the variables as a,b,c,d,etc.

you can simply declare all these letters first. Once you have done that you can use for each loop as follows

Code

            int a, b, c, d, e, f, g, h, i, j, k, l;

// conversion of whole value in text box to single integers
            char[] digits_array = TextBox1.Text.ToCharArray(); 

//Now just declare each variable as you want there are several ways to do it    
            a = int.Parse(digits_array[0].ToString());
            b = int.Parse(digits_array[1].ToString());
            c = int.Parse(digits_array[2].ToString());
            d = int.Parse(digits_array[3].ToString());
            e = int.Parse(digits_array[4].ToString());
            f = int.Parse(digits_array[5].ToString());
            g = int.Parse(digits_array[6].ToString());
            h = int.Parse(digits_array[7].ToString());
            i = int.Parse(digits_array[8].ToString());
            j = int.Parse(digits_array[9].ToString());
            k = int.Parse(digits_array[10].ToString());
            l = int.Parse(digits_array[11].ToString());

Now You can simply use these values in your Formaula the main function that i used here is .ToCharArray() Function . I have checked the script and its working fine Hoever the script seems pretty Long So I want other developers to help me out to squeeze this code. I tried Plenty of things apart from this method but none of them worked.

like image 120
Abdul Hannan Avatar answered Apr 30 '26 15:04

Abdul Hannan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!