Main Site Documentation

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


#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.

http://msdn.microsoft.com/en-us/library/xfw01fx4(v=vs.85).aspx

] 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.


#2

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,
http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx#Y3063
It has both VB and VC# sample that did the same task.

Hope this help
sam


#3

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

http://msdn.microsoft.com/en-us/library/cc532127.aspx

Andrew


#4

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

http://www.developerfusion.com/tools/convert/vb-to-csharp/

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;

}

#5

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


#6

Stan45, how’s it going?


#7

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