-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

WatsMyName
  • 4,240
  • 5
  • 42
  • 73

1 Answers1

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

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • 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? – WatsMyName Nov 27 '13 at 16:57