?
Solved

Convert vb.net (2003) to c# (2005)

Posted on 2007-03-19
4
Medium Priority
?
693 Views
Last Modified: 2012-08-13
Hello Experts,
Can anyone help me conver the following vb.net 2003 code to c# 2005 ?
Thanks

   Public shared Function MD5HashHex(ByVal InString As String) as String
                        For SrcIdx = 0 To InString.Length() - 1
                            Data(DstIdx) = AscW(InString.Substring(SrcIdx, 1)) And &H00FF
                            DstIdx += 1

                            Data(DstIdx) = (AscW(InString.Substring(SrcIdx, 1)) And &HFF00) >> 8
                            DstIdx += 1
                        Next

                        Dim Result As Byte() = md5.ComputeHash(Data)
                        Dim SResult as String = ""

                        For DstIdx=0 to Result.Length() - 1
                            SResult = Microsoft.VisualBasic.Format(Result(DstIdx), "X").PadLeft(2, "0") & SResult
                        Next

                        Return SResult
                    End Function
0
Comment
Question by:svaria
  • 2
4 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 18749989
public static string MD5HashHex(string InString)
{
 for (int SrcIdx = 0; SrcIdx <= InString.Length() - 1; SrcIdx++) {
   Data(DstIdx) = AscW(InString.Substring(SrcIdx, 1)) & 255;
   DstIdx += 1;
   Data(DstIdx) = (AscW(InString.Substring(SrcIdx, 1)) & 65280) >> 8;
   DstIdx += 1;
 }
 byte[] Result = md5.ComputeHash(Data);
 string SResult = "";
 for (int DstIdx = 0; DstIdx <= Result.Length() - 1; DstIdx++) {
   SResult = Microsoft.VisualBasic.Format(Result(DstIdx), "X").PadLeft(2, "0") + SResult;
 }
 return SResult;
}

For future reference, there is a little online tool for converting c# to vb.net and viceversa:
http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
0
 

Author Comment

by:svaria
ID: 18750020
Did you complie the code ?
I got 14 errors on that.
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 18750100
there is nothing wrong with the conversion itself..it's not a fully "encapsulated" function and is using variables I assumed are declared publicly somewhere else on the page or class file...
in both versions, there are a bunch of external variables and references to the variables that are causing the errors...
SrcIdx,DstIdx,Data and md5

0
 
LVL 13

Accepted Solution

by:
Webstorm earned 1500 total points
ID: 18750202

try this:

using System.Security.Cryptography;

public static string MD5HashHex(string InString)
{
    byte[] bytes = Encoding.UTF8.GetBytes(InString);
    // or Encoding.ASCII.GetBytes(InString);

    HashAlgorithm hash = new MD5CryptoServiceProvider();
    byte[] hashBytes = hash.ComputeHash( bytes );

    StringBuilder sb=new StringBuilder();
    foreach(byte b in hashBytes)
         sb.Append( b.ToString("X2") );
    return sb.ToString();
}


Or you can simply let it in vb.net, compiling it as a DLL assembly which you use when compiling your C# project.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question