Solved

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

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

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. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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