Help converting java-script to C#.
-
Hello! i'm a bit baffled when it comes to converting this javascript over to C#... Any help would be appreciated! Thank you :) Here is the javascript:
function d(strInput) {
strInput = decoder(strInput);
var strOutput = "";
var intOffset = (key + 112) / 12;
for (i = 4; i < strInput.length; i++) {
thisCharCode = strInput.charCodeAt(i);
newCharCode = thisCharCode - intOffset;
strOutput += String.fromCharCode(newCharCode)
}
document.write(strOutput)
}And this is what i attempted at converting to C#.
public string decode(int key, string data) { int i; string strInput = base64Decode(data); string strOutput = ""; int intOffset = (key + 112) / 12; for (i = 4; i < strInput.Length; i++) { char thisLetter = strInput\[i\]; char thisCharCode = strInput\[i\]; int newCharCode = thisCharCode - intOffset; strOutput += new String(new char\[\]{Convert.ToChar(newCharCode)}); } return strOutput; }
-
Hello! i'm a bit baffled when it comes to converting this javascript over to C#... Any help would be appreciated! Thank you :) Here is the javascript:
function d(strInput) {
strInput = decoder(strInput);
var strOutput = "";
var intOffset = (key + 112) / 12;
for (i = 4; i < strInput.length; i++) {
thisCharCode = strInput.charCodeAt(i);
newCharCode = thisCharCode - intOffset;
strOutput += String.fromCharCode(newCharCode)
}
document.write(strOutput)
}And this is what i attempted at converting to C#.
public string decode(int key, string data) { int i; string strInput = base64Decode(data); string strOutput = ""; int intOffset = (key + 112) / 12; for (i = 4; i < strInput.Length; i++) { char thisLetter = strInput\[i\]; char thisCharCode = strInput\[i\]; int newCharCode = thisCharCode - intOffset; strOutput += new String(new char\[\]{Convert.ToChar(newCharCode)}); } return strOutput; }
Firstly user StringBuilder and use append method for adding strings. It will improve performance. Second where is char thisLetter used? Third what is the error you are getting? Fourth why use Convert.ToChar when it already a character. Ahsan Ullah Senior Software Engineer MCTS 2.0
-
Firstly user StringBuilder and use append method for adding strings. It will improve performance. Second where is char thisLetter used? Third what is the error you are getting? Fourth why use Convert.ToChar when it already a character. Ahsan Ullah Senior Software Engineer MCTS 2.0
Well, i got it to work only when the key is a negative number.. For some reason it's still not able to convert when data is a positive number. Example: (int key = 212, string data = "U0lra36DfImFkImOkImCW4OKj4h8hIdJfoqI") Output = {c¬a¬¬¬¬¬¬¬¬@¬¬¬¬a¬¬.c¬¬} Example2: (int key = -88, string data = "T1RXYmV0cHFkZ3R1MzQ1Ng==") Output = {crnobers1234} Code:
public string decode(int key, string data)
{
int i;
string strInput = base64Decode(data);
StringBuilder strOutput = new StringBuilder("");int intOffset = (key + 112) / 12; for (i = 4; i < strInput.Length; i++) { int thisCharCode = strInput\[i\]; char newCharCode = (char)(thisCharCode - intOffset); strOutput.Append(newCharCode); } return strOutput.ToString(); }
-
Hello! i'm a bit baffled when it comes to converting this javascript over to C#... Any help would be appreciated! Thank you :) Here is the javascript:
function d(strInput) {
strInput = decoder(strInput);
var strOutput = "";
var intOffset = (key + 112) / 12;
for (i = 4; i < strInput.length; i++) {
thisCharCode = strInput.charCodeAt(i);
newCharCode = thisCharCode - intOffset;
strOutput += String.fromCharCode(newCharCode)
}
document.write(strOutput)
}And this is what i attempted at converting to C#.
public string decode(int key, string data) { int i; string strInput = base64Decode(data); string strOutput = ""; int intOffset = (key + 112) / 12; for (i = 4; i < strInput.Length; i++) { char thisLetter = strInput\[i\]; char thisCharCode = strInput\[i\]; int newCharCode = thisCharCode - intOffset; strOutput += new String(new char\[\]{Convert.ToChar(newCharCode)}); } return strOutput; }
Solved: <pre>public string decoder(string data) { string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; char o1, o2, o3; int h1, h2, h3, h4, bits, i = 0; string enc = ""; do { h1 = b64.IndexOf(data.Substring(i++, 1)); h2 = b64.IndexOf(data.Substring(i++, 1)); h3 = b64.IndexOf(data.Substring(i++, 1)); h4 = b64.IndexOf(data.Substring(i++, 1)); bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; o1 = (char)(bits >> 16 & 0xff); o2 = (char)(bits >> 8 & 0xff); o3 = (char)(bits & 0xff); if (h3 == 64) enc += new string(new char[] { o1 }); else if (h4 == 64) enc += new string(new char[] { o1, o2 }); else enc += new string(new char[] { o1, o2, o3 }); } while (i < data.Length); return enc; } public string d(int key, string data) { int i; string strInput = decoder(data); StringBuilder strOutput = new StringBuilder(""); int intOffset = (key + 112) / 12; for (i = 4; i < strInput.Length; i++) { int thisCharCode = strInput[i]; char newCharCode = (char)(thisCharCode - intOffset); strOutput.Append(newCharCode); } return strOutput.ToString(); } </pre>