How to change this VB Function into C# (MF 4.1)

Hello Guys,

I am new to programming by using C#.

I try to change this function into a C# function.
However, some of the methods are not supported in C#.

For example, I can’t really find the " Asc() " [ According to MSDN : Returns the ANSI character code corresponding to the first letter in a string.

] in C#.

So I planned to use this “Convert.ToInt32()” to solve this problem, but for some reasons I can’t find this in MF4.1.


[b] Public Function WriteStr(ByRef Str As String) As Boolean

          Dim SData(Str.Length - 1) As Byte
          Dim i As Long

          WriteStr = True          
          SockErr = 0                 

         
             For i = 0 To Str.Length - 1
                 SData(i) = Asc(Str.Chars(i))
          Next i

          WriteStr = WriteBin(SData, Str.Length)

   End Function[/b]

Thank you for your concern.

So I planned to use this “Convert.ToInt32()” to solve this problem, but for some reasons I can’t find this in MF4.1.

You could use


Int32.Parse(String);
or
Int32.Parse(String, NumberStyles);
or
Int32.Parse(String, IFormatProvider) ;
or
Int32.Parse(String, String, NumberStyles, IFormatProvider) ;

Her is the link that worth looking at,
[url]Int32.Parse Method (System) | Microsoft Learn
It has both VB and VC# sample that did the same task.

Hope this help
sam

Looks to me like UTF8Encoding.GetBytes() is what you want. Turns a string into an array of bytes.

Andrew

I use sometime the following website to convert vb.net to c#.net code:

Always working :wink:

So you would get the following c#.net function:

public bool WriteStr(ref string Str)
{
	bool functionReturnValue = false;

	byte[] SData = new byte[Str.Length];
	long i = 0;

	functionReturnValue = true;
	SockErr = 0;


	for (i = 0; i <= Str.Length - 1; i++) {
		SData[i] = Strings.Asc(Str[i]);
	}

	functionReturnValue = WriteBin(SData, Str.Length);
	return functionReturnValue;

}

public bool WriteStr(string str)
{
	byte[] sData = Encoding.UTF8.GetBytes(str);
	return (WriteBin(sData, sData.Length));
}

Stan45, how’s it going?

Hello guys,

Thank you all for the help.

I could convert that VB function into a C# function.

Sorry about replying late.

I am currently working my graduation project.

I may come up here to ask more questions regarding MF4.1 and C#.

Thank you again for the help~! ;D