Solved

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

Posted on 2007-03-19
4
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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