Help converting Vb.Net to C#

J

jj

It seems simple but I had a hard time converting this small function from
vb.net to C#. I even tried the
software that automatically converts from vb to c# , but to no avail.
Please can someone take some precious minutes to help me out.
Thanks
John

Public Function SimpleCrypt( ByVal Text As String) As String

Dim strTempChar As String, i As Integer

For i = 1 To Len(Text)

If Asc(Mid$(Text, i, 1)) < 128 Then

strTempChar = CType(Asc(Mid$(Text, i, 1)) + 49, String)

ElseIf Asc(Mid$(Text, i, 1)) > 128 Then

strTempChar = CType(Asc(Mid$(Text, i, 1)) - 49, String)

End If

Mid$(Text, i, 1) = Chr(CType(strTempChar, Integer))

Next i

Return Text

End Function
 
F

Fredrik Wahlgren

jj said:
It seems simple but I had a hard time converting this small function from
vb.net to C#. I even tried the
software that automatically converts from vb to c# , but to no avail.
Please can someone take some precious minutes to help me out.
Thanks
John

Truncated

What conversion tool did you use? I have tried something called "VB.Net To
C# Converter"
Do you get some kind of message that indicates the problem?

/ Fredrik
 
G

Guest

jj,

Try this. I'm a little confused, because it seems that you're both
returning the encrypted string and modifying the parameter, but this may be a
lack of my VB-reading skills. If that's what you wanted, call it like this:
string original = "Hello, world";
string encrypted = SimpleCrypt(ref strOriginal); // now both are encrypted

If you really only wanted to return the encrypted string and leave the
original alone, nuke the 'ref' and the line marked "// modify argument?"

public string SimpleCrypt (ref string inputText)
{
// requires using System.Text
StringBuilder result = new StringBuilder();
for (int ch = 0; ch < inputText.Length; ch++)
{
char encrypted;
int ordNextChar = (int) inputText[ch];
if (ordNextChar < 128)
encrypted = Convert.ToChar(ordNextChar + 49);
else if (ordNextChar > 128)
encrypted = Convert.ToChar(ordNextChar - 49);
else // 128 exactly
encrypted = Convert.ToChar(ordNextChar);

result.Append(encrypted);
}

// modify argument?
inputText = result.ToString();

return (result.ToString());
}

Good luck,

PC
 
K

Kevin Aubuchon

Try this:
public static string SimpleCrypt(string Text)
{
byte [] textBytes = new byte[Encoding.ASCII.GetByteCount(Text)];

textBytes = Encoding.ASCII.GetBytes(Text);

for (int i=0; i<textBytes.Length; i++)
{
if (textBytes < 128)
textBytes = Convert.ToByte(textBytes + 49);
else
textBytes = Convert.ToByte(textBytes - 49);
}

return Encoding.ASCII.GetString (textBytes);

}

kevin aubuchon
www.aubuchon-design.com
 
G

Guest

If you want a literal conversion to C# (not necessarily the best way of doing
this), then our Instant C# VB.NET to C# converter (www.instantcsharp.com)
produces:

(note: 'VBCasts' is a static class used by Instant C# to emulate the VB cast
macros - the code is viewable from the Demo Edition)

public string SimpleCrypt(string Text)
{
string strTempChar = null;
int i = 0;
int ForTemp1 = Text.Length;
for (i = 1; i <= ForTemp1; i++)
{
if (System.Convert.ToInt32(Text.Substring(i - 1, 1)) < 128)
{
strTempChar = ((string)(System.Convert.ToInt32(Text.Substring(i - 1, 1)) +
49));
}
else if (System.Convert.ToInt32(Text.Substring(i - 1, 1)) > 128)
{
strTempChar = ((string)(System.Convert.ToInt32(Text.Substring(i - 1, 1)) -
49));
}
Text = Text.PadRight(Text.Length + 1).Remove(i - 1, 1).Insert(i - 1,
((char)(VBCasts.CInt(strTempChar))).ToString()).Substring(0, Text.Length);
}
return Text;
}
 
C

Cor Ligthert

JJ,

My try not really completly tested.

\\\
public static string BadlyCrypt(string text)
{
System.Text.StringBuilder sb =
new System.Text.StringBuilder();
for (int i = 0; i < text.Length ; i++)
{
int b = (int)text;
if (b < 128)
{b += 49;}
else if (b > 128)
{b -= 49;}
sb.Append(Convert.ToChar(b));
}
return sb.ToString();
}
///
Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top