.net - Remainder operator in C# perform different than MOD in Visual Basic -


i have found strange situation converting piece of code c# vb.net, code small class convert base 10 base 36 , vice versa.

the key point function :

    /// <summary>     /// base36 de- , encoder     /// </summary>     public static class base36     {         private const string charlist = "0123456789abcdefghijklmnopqrstuvwxyz";          /// <summary>         /// encode given number base36 string         /// </summary>         /// <param name="input"></param>         /// <returns></returns>         public static string encode(long input)         {             if (input < 0) throw new argumentoutofrangeexception("input", input, "input cannot negative");              char[] clistarr = charlist.tochararray();             var result = new stack<char>();             while (input != 0)             {                 result.push(clistarr[input % 36]);                 input /= 36;             }             return new string(result.toarray());         } 

that converted in vb.net should result in :

public notinheritable class base36     private sub new()     end sub     private const charlist string = "0123456789abcdefghijklmnopqrstuvwxyz"      ''' <summary>     ''' encode given number base36 string     ''' </summary>     ''' <param name="input"></param>     ''' <returns></returns>     public shared function encode(input int64) string         if input < 0             throw new argumentoutofrangeexception("input", input, "input cannot negative")         end if          dim clistarr char() = charlist.tochararray()         dim result = new stack(of char)()         while input <> 0             result.push(clistarr(input mod 36))             input /= 36         end while         return new string(result.toarray())     end function 

the problem modulo operator in vb.net perform differently % remainder operator in c#, in fact if call encode method in c# :

        long l = 13072113072399;          string result = base36.encode(l);   //result : 4mt8um0b3 

while calling method in c# :

    dim l int64 = 13072113072399     dim result string = base36.encode(l) //result : 5nujsu3ar 

the responsible of difference different result modulo operator return in situations, why ?

what equivalent of % remainder operator in vb.net ?

the mod operator:

clistarr(input mod 36) 

but actual issue is

input /= 36 

in c#, / integer division when used on 2 ints. in vb.net, / double on integers , uses bankers’ rounding. change integer division:

input \= 36 

or use divmod properly:

input = math.divrem(input, 36, remainder) 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -