Solved

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

Posted on 2007-03-19
4
657 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 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now