[Mono-devel-list] VB ... Very difficult to translate to C# !!!

A Rafael D Teixeira rafaelteixeirabr at hotmail.com
Thu Jul 8 14:53:19 EDT 2004


First of all, here an (untested) simplified version in VB.NET

Option Strict On

Public Class DCUtils

    Private Function AssemblePart(ByVal h As Integer) As String
        h = ((h \ 16) Xor (h * 16)) And 255
        Select Case h
            Case 0, 5, 36, 96, 124, 126
                Return String.Format("/%DCN{0:000}%/", h)
            Case Else
                Return Chr(h)
        End Select
    End Function

    Public Function LockToKey(ByVal Lck As String) As String
        Dim j As Integer

        Lck = Lck.Split(" "c)(0)

        LockToKey = AssemblePart(Asc(Lck) Xor Asc(Right$(Lck, 1)) Xor 
Asc(Right$(Lck, 2)) Xor 5)

        For j = 2 To Len(Lck)
            LockToKey = LockToKey & AssemblePart(Asc(Mid$(Lck, j, 1)) Xor 
Asc(Mid$(Lck, j - 1, 1)))
        Next
    End Function

End Class

which converted to C# may become

using System;

namespace Yeah
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class DCUtils
	{
		private static int Xor(params char[] values)
		{
			int h = (int)values[0];
			for (int j = 1; j < values.Length; j++)
				h ^= (int)values[j];
			return h;
		}

		private static string AssemblePart(int h)
		{
			h = ((h / 16) ^ (h * 16)) & 255;
			switch (h)
			{
				case 0: case 5: case 36: case 96: case 124: case 126:
					return String.Format("/%DCN{0:000}%/", h);
				default:
					System.Text.StringBuilder sb = new System.Text.StringBuilder(1);
					sb.Append((char)h);
					return sb.ToString();
			}
		}

		public static string LockToKey(string Lck)
		{
			Lck = Lck.Split(' ')[0];

			string result = AssemblePart(Xor(Lck[0], Lck[Lck.Length-1], 
Lck[Lck.Length-2], (char)5));

			for(int j = 1; j < Lck.Length; j++)
				result = result + AssemblePart(Xor(Lck[j], Lck[j - 1]));

			return result;
		}
	}
}

All 3 versions (the old VB6 one and those new 2) run the same result for my 
test pattern "Rumba 3"

Hope it helps


Rafael "Monoman" Teixeira
Mono Hacker since 16 Jul 2001 - http://www.go-mono.org/
MonoBrasil Founding Member - Membro Fundador do MonoBrasil 
http://monobrasil.softwarelivre.org
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/




>From: Alessandro Torrisi <alessandro.torrisi at eurone.it>
>Reply-To: alessandro.torrisi at eurone.it
>To: mono-devel-list at lists.ximian.com
>Subject: [Mono-devel-list] VB ... Very difficult to translate to C# !!!
>Date: Thu, 08 Jul 2004 17:28:24 +0200
>
>Do you know how can I translate this in C# code ???
>I have very difficulties...
>
>============================================================================
>
>Public Class DCUtils
>
>     Public Function LockToKey(ByVal Lck As String) As String
>         Dim h As Integer, j As Integer, n As Integer
>         n = 5
>
>         h = InStr(1, Lck, " ")
>         If h Then Lck = Left$(Lck, h - 1)
>
>         h = Asc(Lck) Xor Asc(Right$(Lck, 1)) Xor Asc(Right$(Lck, 2)) Xor
>n
>         h = (h \ 16) Xor (h * 16)
>
>         Do While h > 255
>             h = h - 256
>         Loop
>
>         Select Case h
>             Case 0, 5, 36, 96, 124, 126
>                 LockToKey = "/%DCN" & Right$("00" & CStr(h), 3) & "%/"
>             Case Else
>                 LockToKey = Chr(h)
>         End Select
>
>         For j = 2 To Len(Lck)
>             h = Asc(Mid$(Lck, j, 1)) Xor Asc(Mid$(Lck, j - 1, 1))
>             h = (h \ 16) Xor (h * 16)
>
>             Do While h > 255
>                 h = h - 256
>             Loop
>
>             Select Case h
>                 Case 0, 5, 36, 96, 124, 126
>                     LockToKey = LockToKey & "/%DCN" & Right$("00" &
>CStr(h), 3) & "%/"
>                 Case Else
>                     LockToKey = LockToKey & Chr(h)
>             End Select
>         Next
>     End Function
>
>End Class
>
>============================================================================
>
>Please help ME !!! Alex.
>P.S. Compiling it with MBAS it took some error !!!
>
>_______________________________________________
>Mono-devel-list mailing list
>Mono-devel-list at lists.ximian.com
>http://lists.ximian.com/mailman/listinfo/mono-devel-list

_________________________________________________________________
MSN Messenger: instale grátis e converse com seus amigos. 
http://messenger.msn.com.br




More information about the Mono-devel-list mailing list