change function from vb c#

This function must be changed by a c# expert the converter does not work. It converts this function but the Conversion  takes Len(text)  into Strings.Len(text) in which string does not exists in the current context.  

Function MakeByte(ByVal text As String) As Byte()

        Dim i As Integer
        Dim ba(text.Length - 1) As Byte
        For i = 1 To Len(text)
            ba(i - 1) = Asc(Mid(text, i, 1))
        Next

        Return ba

    End Function

Open in new window

LVL 9
Seven priceFull StackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ken ButtersCommented:
you can change it back to len(text)

Both are valid. difference is len(text) will return 0 on null string
0
käµfm³d 👽Commented:
If I'm reading your function correctly, then there's already built-in functionality to accomplish this:

e.g.

using System.Text;

...

byte[] converted = Encoding.ASCIIEncoding.GetBytes("your string");

Open in new window

0
Seven priceFull StackAuthor Commented:
The above code is in vb. trying to convert to c#
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Seven priceFull StackAuthor Commented:
see below c# version but Strings.Len and Strings.asc Error string does not exists in the current context.  

public byte[] MakeByte(string text)
{

	int i = 0;
	byte[] ba = new byte[text.Length];
	for (i = 1; i <= Strings.Len(text); i++) {
		ba[i - 1] = Strings.Asc(Strings.Mid(text, i, 1));
	}

	return ba;

}

Open in new window

0
käµfm³d 👽Commented:
What I am saying is that you don't need to create a function to do this--it already exists.
0
hongjunCommented:
Try one of the following:

1. Conversion to C#. It's important to check if the text is empty first.

byte[] MakeByte(string text)
{
    int i;
    byte[] ba = null;

    if (!String.IsNullOrEmpty(text))
    {
        ba = new byte[text.Length];
        for (i = 0; i < ba.Length; i++)
            ba[i] = (byte)text.Substring(i, 1).ToCharArray()[0];
    }

    return ba;
}

Open in new window


2. kaufmed has already pointed out there is an existing method to do it.

byte[] converted2 = System.Text.Encoding.UTF8.GetBytes("hello");

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
Do not try to jump straight into C# from VB. Read a bit about the basics to prevent many simple errors.

Where do you get that Strings from?

C# is sensible to case, and in C#, string is all lowercase, no s at the end.

To get the length of a string variable, you simply call it's Len method:

for (i = 1; i <= text.Length; i++)

A string can be treated as an array of char. So to get a character in a string, simply go:

ba[x] = text[x];

Putting it all together:

public byte[] MakeByte(string text)
{

   int i;
   byte[] ba = null;

   if ( !String.IsNullOrEmpty ( text ) )
   {
      ba = new byte[text.Length];
      for ( i = 0; i < ba.Length; i++ )
      ba[i] = (byte) text[i];
   }

   return ba;

} 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackAuthor Commented:
thanks again.
0
käµfm³d 👽Commented:
I would challenge you to compare the results of your new function and the one I posted above  : )
0
Jacques Bourgeois (James Burger)PresidentCommented:
Yes, the results are the same.

But results are not all. This comes from my experience as a trainer, specially with programmers that are making the move from classic VB to VB.NET or "worse", to C#.

It's clear that sevensnake is beginning to work in C# and needs a better understanding of the basics. Usually, beginners who are still in the learning process like to be told what was wrong with their code from the start, not only get a working solution.

And at that stage, a beginner might prefer to deal with code that he can relate to from his previous experience than try to understand stuff like "encoding", a word or concept that he might never have encountered before. Believe me, a lot of programmers have always worked in a closed Windows environment and never had to deal with encoding, so they do not know what this means.

I also know that VB programmers often have problems getting rid of the Mid method, so I offered an easy way of retrieving a unique character from a string.
0
käµfm³d 👽Commented:
Part of learning the Framework is knowing what it provides  ; )

If it was for the sake of learning, I did not see that mentioned. Had it been, I would have had no problem doing the conversion (I just did a conversion for the OP in a previous question). I got the impression that this was a "I need to turn this code into C# by XXX deadline" question.

As professional developers, we strive to reduce redundancy. Such reductions aid testing and maintainability. Learning to be a good developer encompasses far more than just learning syntax, as I'm sure you're aware  = )

C'est la vie. The question has been answered, and nobody lost a limb. A good day for everyone, I suppose.

Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.