473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help converting Vb.Net to C#

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
Nov 16 '05 #1
7 2040

"jj" <yh*******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2
jj
I used the utility on this link but didn't work:

http://developerfusion.com/utilities...btocsharp.aspx
"jj" <yh*******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #3
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

Nov 16 '05 #4
Try this:
"jj" <yh*******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #5
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[i] < 128)
textBytes[i] = Convert.ToByte(textBytes[i] + 49);
else
textBytes[i] = Convert.ToByte(textBytes[i] - 49);
}

return Encoding.ASCII.GetString (textBytes);

}

kevin aubuchon
www.aubuchon-design.com
"jj" <yh*******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #6
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()).Su bstring(0, Text.Length);
}
return Text;
}
"jj" wrote:
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

Nov 16 '05 #7
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[i];
if (b < 128)
{b += 49;}
else if (b > 128)
{b -= 49;}
sb.Append(Convert.ToChar(b));
}
return sb.ToString();
}
///
Cor
"jj" <yh*******@yahoo.com>
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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rex_chaos | last post by:
Hi all, I have a question about datatype converting. Consider the following types std::complex<double> and struct MyComplex { double re, im; }
4
by: jipster | last post by:
hi, i need help converting this java program to C++...are there any programs or nething that could be used to do this faster?? class hw2 { static public void main (String args) { hw2.test();}...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
3
by: alyssa | last post by:
Hi guys, May i know how to declare a string of binary data and pass it to the method? For example, int a={10010001120420052314} is it correct? and if i have receive the binary data, may i know...
4
by: Clark Stevens | last post by:
I have a program that I'm converting from VB6 to VB.NET. It reads in a text file containing barcode numbers and their corresponding descriptions. Then the user enters the barcode number and the...
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
2
anukagni
by: anukagni | last post by:
Hi, iam having an database for that i have created an user manual includes help topics ..I prepared in the word format and i want to covert this to html help . Iam having Ms Html workshop but i...
10
by: minterman | last post by:
1. the program needs to convert btu to jules 2. Convert calories to joules 3. Convert joules to joules 4 exit the program. if the user type anything other than 1-4 the program should print a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.