user3356534
user3356534

Reputation:

How to Convert Javascript Code to C# Code

I have a Javascript method. I wrote on C# but it dosen't work.

Javascript Code

var __AM = 65521;
    function cc(a) {
        var c = 1, b = 0, d, e;
        for (e = 0; e < a.length; e++) {
            d = a.charCodeAt(e);
            c = (c + d) % __AM;
            b = (b + c) % __AM;
        }
        return b << 16 | c;
    }

My written C# Code

        private string CC(string a)
    {
        var __AM = 65521;
        int e;
        long d;
        long c = 1, b = 0;
        for (e = 0; e < a.Length; e++)
        {
            var p = Encoding.Unicode.GetBytes(a[e].ToString());
            d = Convert.ToInt32(p.First());
            c = (c + d) % __AM;
            b = (b + c) % __AM;
        }
        return b.ToString() + c.ToString();
    }

JS Test

cc("4JipHEz53sU1406413803");

Result: 1132332429

C# Test

CC("4JipHEz53sU1406413803");

Result: 172781421

How Can I get JS value in C#?

Upvotes: 3

Views: 1094

Answers (1)

Cory
Cory

Reputation: 1802

This code works:

private string cc(string a)
{
    var __AM = 65521;
    int e;
    long d;
    long c = 1, b = 0;
    for (e = 0; e < a.Length; e++)
    {
        d = (int)a[e];
        c = (c + d) % __AM;
        b = (b + c) % __AM;
    }
    return (b << 16 | c).ToString();
}

Upvotes: 2

Related Questions