C#C
C#4y ago
TerryDev

✅ Need some help converting PHP code to CS

Hey, I think I wrote this right but I'm getting different results. Perhaps someone knows what I did wrong here?

I'm trying to convert a PHP function to a C# function. Here is what I wrote in C#. The original PHP snippets are the comments above.

string tobase64str(string input, int count)
{
    string output = "";
    int i = 0;
    string itoa64 = _password_itoa64();
    do
    {
        //string value = ord($input[$i++]);
        var value = input[i++];
        //$output.= $itoa64[$value & 0x3f];
        output += itoa64[value & 0x3f];
        //if ($i < $count) {
        if (i < count)
        {
            //$value |= ord($input[$i]) << 8;
            value = ((char)(value | input[i] << 8));
        }
        //$output.= $itoa64[($value >> 6) & 0x3f];
        output += itoa64[(value >> 6) & 0x3f];
        //if ($i++ >= $count) {
        if (i++ >= count)
        {
            break;
        }
        //if ($i < $count) {
        if (i < count)
        {
            //$value |= ord($input[$i]) << 16;
            value = (char)(value | input[i] << 16);
        }
        //$output.= $itoa64[($value >> 12) & 0x3f];
        output += itoa64[(value >> 12) & 0x3f];
        //if ($i++ >= $count) {
        if (i++ >= count)
        {
            break;
        }
        //$output.= $itoa64[($value >> 18) & 0x3f];
        output += itoa64[(value >> 18) & 0x3f];
    } while (i < count);

    return output;
}

string _password_itoa64()
{
    return "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}


Upon running this, I do not get the expected result of:
VrOlH8EFHZWQeyrP2on5MR7Q8QMC08ZnHfLv8UE54ZwL7CIJ6vKhT3EpdYriab.oA/yv9wP/yyWUHQC9.18y0$S$D

Instead, I get:
/..l1..F1..Q0..P...5...Q0..C0..n1..v0..50..L/..J...h1..p/..i0..o...v1../0..U1..9...y0
Was this page helpful?