Converting code - different output
Hi. im trying to convert this from VB6/classic ASP to C# but the result for some areas are different:
Code:
strPassword = LCase(strPassword)
For iIdx = 1 To Len(strPassword)
strPassword2 = strPassword2 & Chr(Asc(Mid(strPassword, iIdx, 1)) + 13)
Next
in C#, this seems to be the following:
Code:
string rawData = rawPass.ToLower();
string encrypted = string.Empty;
for (var iIdx = 1; iIdx <= rawData.Length; iIdx++)
{
encrypted = encrypted + (char)(Convert.ToInt32(rawData[iIdx - 1]) + 13);
}
return encrypted;
However, in certain circumstances, the passwords seems to differ in the output.
is the code conversion incorrect?
test case: Tresc@l41
Re: Converting code - different output
Strings in .NET are Unicode, so you can't treat them exactly the same as ASCII strings where chars and bytes were largely interchangable. I would determine which encoding you want to use (probably Encoding.ASCII) and get the bytes for the string (GetBytes() method) and do operations on those bytes.
Re: Converting code - different output
true but the point here is this:
works fine in VB.NET but not in C#
Re: Converting code - different output
If the first post was showing Vb.NET code (I understood it to be VB6) then I'd assume that those legacy functions are internally working on the string as though they were ASCII encoded. Which means they are not doing the same thing as the string indexer/Convert.ToInt32/cast to char that the C# code is doing.
The C# version is a really bad way of working with strings. I can't really speak to the legacy VB functions version, except to wonder how they deal with strings that can't be represented in ASCII?
Re: Converting code - different output
The VB.NET code is using the legacy VB functions which are loyal to VB classic. VB classic in its more recent incantations used unicode internally but these were covered with the wide character functions in this case ChrW and AscW.
As Evil G suggests I'm pretty sure you will need to convert the string to ASCII which (as far as my sketchy memory permits) will replace none representable characters with ? (63) just like those double legacy functions Chr and Asc.
Re: Converting code - different output
I would write it like this:
csharp Code:
string str1 = "Testing";
string result = string.Empty;
str1 = str1.ToLower();
for (int i = 0; i < str1.Length; i++)
{
result += (char)(str1[i] + 13);
}
Console.WriteLine(result);
I'm not seeing what you mean by the output is different though? Maybe you have one project console set to Raster fonts and another set to Lucida or something?