PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Converting code - different output-VBForums
Results 1 to 6 of 6

Thread: Converting code - different output

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Converting code - different output

    true but the point here is this:

    works fine in VB.NET but not in C#

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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?

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Converting code - different output

    I would write it like this:
    csharp Code:
    1. string str1 = "Testing";
    2. string result = string.Empty;
    3.  
    4. str1 = str1.ToLower();
    5. for (int i = 0; i < str1.Length; i++)
    6. {
    7.     result += (char)(str1[i] + 13);
    8. }
    9. 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?
    Last edited by AceInfinity; Jun 5th, 2013 at 08:34 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width