-1

I m unaware of ruby language. i have the following piece of code in Ruby. I want its c# equivalent. Can anybody help me on this?

def mysql_key(key)
  final_key = "\0" * 16
  key.length.times do |i|
    final_key[i%16] ^= key[i]
  end
  final_key
end

EDIT: This is what I have so far, but its not working

public static int Ord(string str){
            byte[] asciiBytes = Encoding.ASCII.GetBytes(str);
            return Convert.ToInt32(asciiBytes[0]);
        }


       public static string final_key(string key)
        {
            int d = 0;
            int[] Final_key ={0x16} ;
            foreach (char c in key)
            {
                Final_key[d % 16] = Convert.ToChar((Ord(Final_key[d % 16].ToString()) ^ Ord(Final_key[d].ToString())));
                d++;
            }
            return Final_key.ToString();
        }

P.S. I m fairly new in C# as well.

Any help will highly be appreciable Thanks

2
  • You should know we don't do code requests. What have you done so far? Commented Nov 27, 2013 at 4:22
  • Try reading it as if its English... the big differences are the library functions, and they are named very well Commented Nov 27, 2013 at 5:16

1 Answer 1

1

I think you have it essentially right, except ruby's

final_key = "\0" * 16

is the equivalent of

int[] Final_key ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;

And you should probably look into using arrays of bytes instead of all this back and forth ToChar(Ord(ToString...)) stuff

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

1 Comment

Thanks for the reply, I m still not getting into work, Final_key Should be declared string isn't it? as we are converting the while thing to char using Convert.ToChar in a loop?

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.