0

I'm trying to convert a C# script into Python. Here's what the C# Program looks like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Encoder
{
    public static string encodeLength(int value, int length)
    {
        string stack = "";
        for (int x = 1; x <= length; x++)
        {
            int offset = 6 * (length - x);
            byte val = (byte)(64 + (value >> offset & 0x3f));
            stack += (char)val;
        }
        return stack;
    }
    public static string encodeLength(string Val)
    {
        int value = Val.Length;
        int length = 2;
        string stack = "";
        for (int x = 1; x <= length; x++)
        {
            int offset = 6 * (length - x);
            byte val = (byte)(64 + (value >> offset & 0x3f));
            stack += (char)val;
        }
        return stack;
    }
    public static string encodeLength(int value)
    {
        int length = 2;
        string stack = "";
        for (int x = 1; x <= length; x++)
        {
            int offset = 6 * (length - x);
            byte val = (byte)(64 + (value >> offset & 0x3f));
            stack += (char)val;
        }
        return stack;
    }
}

This is the stuff that I get returned when I call Encoder.encodeLength(55) + Encoder.encodeLength("Testing.".Length) + "Testing. in C#.

@w@HTesting.

Here's what I've written in Python so far.

def encodeLength(*args):
    if len(args) == 2 and isinstance(args[0] and args[1], int):
        i = 1
        stack = str()
        while (i <= length):
            offset = 6 * (length - i)
            x = 64 + (value >> offset & 0x3f)
            stack += str(x)
            i +=1
        return stack

    elif len(args) == 1 and isinstance(args[0], str):
        i = 1
        value = args[0]
        length = 2
        stack = str()
        while (i <= length):
            offset = 6 * (length - i)
            x = 64 + (value >> offset & 0x3f)
            stack += str(x)
            i += 1
        return stack

    elif len(args) == 1 and isinstance(args[0], int):
        i = 1
        length = 2
        stack = str()
        while (i <= length):
            offset = 6 * (length - i)
            x = 64 + (args[0] >> offset & 0x3f)
            stack += str(x)
            i +=1
        return stack

This is my response in Python when I call encodeLength(55) + encodeLength(len("Testing.")) + "Testing.".

641196472Testing.

Does anyone know why this isn't returning the output generated by C#?

3
  • where does length come from? Commented Aug 11, 2014 at 17:53
  • Also, in the C# example your second struing is "Testing" in the python example, your string is "Testing." you have an extra character. Commented Aug 11, 2014 at 18:50
  • WombatPM: That was a type, sorry. Commented Aug 11, 2014 at 19:39

1 Answer 1

4

In Python, str(x) does not convert a char code (ASCII value) into the corresponding char. Use chr(x) to achieve this. str(x) converts objects into strings, meaning that the ASCII code 64 is turned into the string "64", rather than into "@", which is what chr(x) returns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.