Link to home
Start Free TrialLog in
Avatar of Seven price
Seven priceFlag for United States of America

asked on

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

Avatar of Ken Butters
Ken Butters
Flag of United States of America image

you can change it back to len(text)

Both are valid. difference is len(text) will return 0 on null string
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

Avatar of Seven price

ASKER

The above code is in vb. trying to convert to c#
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

What I am saying is that you don't need to create a function to do this--it already exists.
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

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks again.
I would challenge you to compare the results of your new function and the one I posted above  : )
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.
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!