The following function should work (it will output the ASCII character code for a character at a specific index within your string as a string ) :
private string charCodeAt(string yourString, int i)
{
return ((int)yourString[i]).ToString(); //Use i-1 if you want to refer to the i-th character of your string
}
if you want to output this value as an integer, you can simply change it to :
private int charCodeAt(string yourString, int i)
{
return (int)yourString[i]); //Use i-1 if you want to refer to the i-th specific character of a your string
}
Usage :
string result = charCodeAt("test",1); //outputs "101" (as 101 is the ASCII code for 'e')
David, your code will not compile, return type is string but you are trying to return an Int32.
Will the VB in the original post compile? What does VB do when the return type is a string and you give it an Integer, my guess is that it does a ToString() for you but I don't use VB.
If my guesses are right then you would just need to add .ToString() to the end of David's return statement.
Alternatively, the first Rion example with i-1 in place of i.
Here is the original unaltered VB code that I am trying to convert to C# (unless there is a way that i can have a vb file in a C# project. I would prefer it in C# but at this point I could care less........only that it works.
Private Function charCodeAt(ByVal sIn As String, ByVal i As Integer) As String
charCodeAt = Asc(Mid(sIn, i, 1))
Return charCodeAt
End Function
Private Function getDeCryptedPwd(ByVal pwd As String) As String
Dim sCryptedPwd As String = ""
Dim iLenpwd As Integer = Len(pwd)
Dim i As Integer = 1
Dim nChar As Integer
If iLenpwd Mod 2 <> 0 Then Return ""
Try
Do While i < iLenpwd
nChar = (charCodeAt(pwd, i) - 65) * 26
nChar += charCodeAt(pwd, i + 1) - 65
sCryptedPwd = sCryptedPwd & Chr(nChar)
i += 2
Loop
Catch
Return ""
End Try
Return sCryptedPwd
End Function
Based on this additional code, it appears as though you will want your original function (charCodeAt) to return an integer as opposed to a string (since you are using it within mathematical operations) :
//Your original charCode method
private int charCodeAt(string yourString, int i)
{
return ((int)yourString[i]); //Use i-1 if you want to refer to the i-th character of your string
}
private string getDeCryptedPwd(string pwd)
{
string sCryptedPwd = "";
int iLenpwd = pwd.Length;
int i = 1;
int nChar;
if(iLenpwd % 2 != 0) { return ""; }
try
{
while(i < iLenpwd)
{
nChar = (charCodeAt(pwd,i) - 65) * 26;
nChar += (charCodeAt(pwd, i + 1) - 65);
sCryptedPwd += (char)nChar;
i += 2;
}
}
catch
{
return "";
}
return sCryptedPwd;
}
You could likely get rid of some of the extra variables that you have within your function as well :
//Revised Function
private string getDecryptedPassword(string password)
{
//Your variables
string decryptedPassword = "";
int passwordLength = decryptedPassword.Length;
//If password contains an odd number of characters - return ""
if (passwordLength % 2 != 0) { return ""; }
try
{
//Same iteration as previously - pulling two characters at a time and appending them into a single character
for (int i = 0; i < passwordLength; i += 2)
{
//This should handle all of your operations as previously
decryptedPassword += (char)(((charCodeAt(password, i) - 65) * 26) + ((charCodeAt(password, i + 1) - 65)));
}
}
catch
{
//An error occured - return ""
return "";
}
//Returns your decrypted password
return decryptedPassword;
}
I was given this code to use to decrypt passwords in one of our databases, but I don't think this code works at all. I created a VB project and added a reference to it...........it still failed.
I believe the original form GUI that my web will one day replace was written in "C" I believe, so the VB code I was given (see above) was someones failed attempt at conversion.
You can't tell from this but I am very..................[censored] displeased.
It's never fun to inheirit poorly written or sloppy code and maintaining it can often be a stressful part of any software or web developer's job.
Although, it can also present the opportunity to refactor the project (if possible) so that whoever comes behind isn't dealing with this either and if it isn't working at all, then it might be an excellent time to do that (again if possible).
matt.gulick
Member
75 Points
86 Posts
Need help converting a very small piece of VB to C#
Feb 05, 2013 07:29 PM|LINK
I have tried conversion at several sites but they all have an issue with Strings.Asc and the code does not work.
Private Function charCodeAt(ByVal sIn As String, ByVal i As Integer) As String charCodeAt = Asc(Mid(sIn, i, 1)) Return charCodeAt End FunctionI would just add it as is to my solution, but it would appear that you can't add vb files to c# projects.
Rion William...
All-Star
31992 Points
5191 Posts
Re: Need help converting a very small piece of VB to C#
Feb 05, 2013 07:42 PM|LINK
The following function should work (it will output the ASCII character code for a character at a specific index within your string as a string ) :
private string charCodeAt(string yourString, int i) { return ((int)yourString[i]).ToString(); //Use i-1 if you want to refer to the i-th character of your string }if you want to output this value as an integer, you can simply change it to :
private int charCodeAt(string yourString, int i) { return (int)yourString[i]); //Use i-1 if you want to refer to the i-th specific character of a your string }Usage :
string result = charCodeAt("test",1); //outputs "101" (as 101 is the ASCII code for 'e')Nasser Malik
Star
12546 Points
1867 Posts
Re: Need help converting a very small piece of VB to C#
Feb 05, 2013 07:53 PM|LINK
Hi Matt,
Asc method is to convert some character to Ascii code
Mid method is equal to SubString
This is an alternative
private string charCodeAt(string sIn, int i) { return Convert.ToInt32(sIn.Substring(i, 1).ToCharArray()[0]).ToString(); }Skype: maleknasser1
David Anton
Contributor
3704 Points
638 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 12:51 AM|LINK
The VB 'Mid' function is 1-based, not 0-based, so the equivalent is:
private string charCodeAt(string sIn, int i) { return System.Convert.ToInt32(sIn[i - 1]); }http://www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
Paul Linton
Star
13581 Points
2571 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 01:46 AM|LINK
David, your code will not compile, return type is string but you are trying to return an Int32.
Will the VB in the original post compile? What does VB do when the return type is a string and you give it an Integer, my guess is that it does a ToString() for you but I don't use VB.
If my guesses are right then you would just need to add .ToString() to the end of David's return statement.
Alternatively, the first Rion example with i-1 in place of i.
David Anton
Contributor
3704 Points
638 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 03:49 AM|LINK
Gotta love VB! It'll let you say the method returns a string and let you return an integer.
http://www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
matt.gulick
Member
75 Points
86 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 11:49 AM|LINK
Here is the original unaltered VB code that I am trying to convert to C# (unless there is a way that i can have a vb file in a C# project. I would prefer it in C# but at this point I could care less........only that it works.
Thanks for your assistance.
Rion William...
All-Star
31992 Points
5191 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 12:33 PM|LINK
Based on this additional code, it appears as though you will want your original function (charCodeAt) to return an integer as opposed to a string (since you are using it within mathematical operations) :
//Your original charCode method private int charCodeAt(string yourString, int i) { return ((int)yourString[i]); //Use i-1 if you want to refer to the i-th character of your string } private string getDeCryptedPwd(string pwd) { string sCryptedPwd = ""; int iLenpwd = pwd.Length; int i = 1; int nChar; if(iLenpwd % 2 != 0) { return ""; } try { while(i < iLenpwd) { nChar = (charCodeAt(pwd,i) - 65) * 26; nChar += (charCodeAt(pwd, i + 1) - 65); sCryptedPwd += (char)nChar; i += 2; } } catch { return ""; } return sCryptedPwd; }You could likely get rid of some of the extra variables that you have within your function as well :
//Revised Function private string getDecryptedPassword(string password) { //Your variables string decryptedPassword = ""; int passwordLength = decryptedPassword.Length; //If password contains an odd number of characters - return "" if (passwordLength % 2 != 0) { return ""; } try { //Same iteration as previously - pulling two characters at a time and appending them into a single character for (int i = 0; i < passwordLength; i += 2) { //This should handle all of your operations as previously decryptedPassword += (char)(((charCodeAt(password, i) - 65) * 26) + ((charCodeAt(password, i + 1) - 65))); } } catch { //An error occured - return "" return ""; } //Returns your decrypted password return decryptedPassword; }matt.gulick
Member
75 Points
86 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 01:51 PM|LINK
I was given this code to use to decrypt passwords in one of our databases, but I don't think this code works at all. I created a VB project and added a reference to it...........it still failed.
I believe the original form GUI that my web will one day replace was written in "C" I believe, so the VB code I was given (see above) was someones failed attempt at conversion.
You can't tell from this but I am very..................[censored] displeased.
Rion William...
All-Star
31992 Points
5191 Posts
Re: Need help converting a very small piece of VB to C#
Feb 06, 2013 02:17 PM|LINK
Sorry to hear that Matt.
It's never fun to inheirit poorly written or sloppy code and maintaining it can often be a stressful part of any software or web developer's job.
Although, it can also present the opportunity to refactor the project (if possible) so that whoever comes behind isn't dealing with this either and if it isn't working at all, then it might be an excellent time to do that (again if possible).