Convert (2003) to c# (2005)

Posted on 2007-03-19
Last Modified: 2012-08-13
Hello Experts,
Can anyone help me conver the following 2003 code to c# 2005 ?

   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

                        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

                        Return SResult
                    End Function
Question by:svaria
  • 2
LVL 37

Expert Comment

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 and viceversa:

Author Comment

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

Expert Comment

ID: 18750100
there is nothing wrong with the conversion'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

LVL 13

Accepted Solution

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, compiling it as a DLL assembly which you use when compiling your C# project.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

808 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