I'm trying to convert the python line of code below to C#:
encoded_c = chr(abs(ord(string[i]) - ord(key_c) % 256))
This is what I got in C#:
char encoded_c=(char)Math.Abs((int)decodedString[i] - (int)key_c % 256);
However, it doesn't return the same result. I think it has to do with the (int) castings, but when I tried searching for other methods of ord() or (int) they didn't seem to work.
For reference , here's the whole block of code:
Python:
def basic_decode(key,string):
decoded_chars=[]
string=base64.urlsafe_b64decode(string)
string=base64.b64decode(string)
for i in xrange(len(string)):
key_c=key[i % len(key)]
encoded_c = chr(abs(ord(string[i]) - ord(key_c) % 256))
decoded_chars.append(encoded_c)
decoded_string="".join(decoded_chars)
return decoded_string
C#:
public string basic_decode(string key,string message)
{
List<int> decoded_chars = new List<int>();
byte[] data = Convert.FromBase64String(message);
string decodedString = Encoding.UTF8.GetString(data);
data = Convert.FromBase64String(decodedString);
decodedString = Encoding.UTF8.GetString(data);
foreach (int i in Enumerable.Range(0, decodedString.Length))
{
char key_c = key[i % key.Length];
char encoded_c=(char)Math.Abs((int)decodedString[i] - (int)key_c % 256); //this line doesnt return what it's supposed to
decoded_chars.Add(encoded_c);
}
string decoded_string = string.Join("",decoded_chars);
return decoded_string;
}
When executing the C# function with:
Console.WriteLine(t.basic_decode("test", "NGNybTU5WE0yQT09"));
Note: NGNybTU5WE0yQT09 : Is an encoded string that was encoded with encode function(With the python version, it encodes and decodes perfectly).
It returns
?
?
?
?
?
?
?
6541765432654186541765417643265418
The question marks refer to:
Console.WriteLine(encoded_c);
Which is located under the char encoded_c=.... line of code
When
print basic_decode("test","NGNybTU5WE0yQT09")
Is executed, it returns:
m
e
s
s
a
g
e
message
message is the correct decoded message
3 Answers 3
Okay, there are two problems that I found with your converted code. First, the second Encoding.Unicode.GetString is throwing off the input. For example, the integer equivalent of the first character in the Python code is 225, but in the C# code it is 65533. You can get around this by skipping the second encoding altogether and just using data.
The second is that you are using a List<int> to store the result characters. When you then use string.Join to create the result string, you are combining a bunch of integers into a string, which is why the output is a bunch of numbers. Change the list to be a List<char> and it will produce the correct result.
Functional code:
public string basic_decode(string key, string message)
{
List<char> decoded_chars = new List<char>();
byte[] data = Convert.FromBase64String(message);
string decodedString = Encoding.UTF8.GetString(data);
data = Convert.FromBase64String(decodedString);
foreach (int i in Enumerable.Range(0, data.Length))
{
char key_c = key[i % key.Length];
char encoded_c = (char)Math.Abs((int)data[i] - (int)key_c % 256);
decoded_chars.Add(encoded_c);
}
string decoded_string = string.Join("", decoded_chars);
return decoded_string;
}
Tested with:
static void Main(string[] args)
{
string dec = basic_decode("test", "NGNybTU5WE0yQT09");
Console.WriteLine(dec);
}
// Prints:
//
// message
3 Comments
int and char might not be obvious.There is an automated online conversion tool you can try here:
I think you know what to expect with an automated conversion. It will probably not compile but will be valid syntax, which is a good enough basis for a manual conversion.
Comments
def sort_list(list_to_sort):
"""
Sorts the given list in ascending order and returns it.
"""
return sorted(list_to_sort)
def reverse_list(list_to_reverse):
"""
Reverses the given list and returns it.
"""
return list_to_reverse[::-1]
my_list = [10, 7, 5, 2, 1]
print("Original list:", my_list)
sorted_list = sort_list(my_list)
print("Sorted list:", sorted_list)
reversed_list = reverse_list(sorted_list)
print("Reversed list:", reversed_list)
basic_decodesupposed to do?"I think it has to do with the (int) castings"- Well, does it? When you debug this, which operation is producing a different result for the same input? There are several operations happening on that one line of code. Break them apart into multiple lines of code. Step through them in a debugger. For any given input, which one produces a different output than in Python? What was that input and what were the two outputs?decodedStringbefore the foreach loop is"�������". Try comparing that to your Python version.