0

I am attempting to create a variable or string from "Convert.ToChar(b[i])" within the following context:

    byte[] b = new byte[100];
    int k = s.Receive(b);
    Console.WriteLine("Recieved...");
    for (int i = 0; i < k; i++)
        Console.Write(Convert.ToChar(b[i]));

E.g.:

var str = Convert.ToChar(b[i]);

But the above does not work, as "i" is not defined within the current context

3
  • 2
    It's not really clear what you mean by "create a variable". Variable names have to be known at compile-time... what are you really trying to achieve? Commented Dec 20, 2011 at 12:46
  • Hi, To get "Convert.ToChar(b[i])" into string format Commented Dec 20, 2011 at 12:47
  • 1
    are you missing some identifier after the var keyword Commented Dec 20, 2011 at 12:47

3 Answers 3

4

Is it just a case of the for loop not being given adequate scope? Try the following...

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
{
    Console.Write(Convert.ToChar(b[i]));
    var myVariable = Convert.ToChar(b[i]);
}

Note that if you didn't include the curly braces, the for loop would only have scope of the first line beneath it, and so the var = Convert.ToChar(b[i]); line would not be able to access the i variable in the loop scope.

That's why I always make sure I put the curly braces in the code for a loop, even if it is for a single line within the loop - it is easy to track the scope of the loop that way.

Sign up to request clarification or add additional context in comments.

2 Comments

Also note that other answers may be more relevant to actually pulling the string out as you intended, but I was purely addressing the scoping issue you had.
+1 for seeing the obvious (other people did not) that the question was dclear, it was just a point of the poster not understanding scoping properly (which happened to many of us in the beginning) so "i" was simply out of scope. AND for being short and precise with the answer.
1

It's fairly unclear what you mean, but it sounds like you might want:

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Received...");
string text = Encoding.ASCII.GetString(b, 0, k);

Note that ASCII isn't the only possible encoding here - the correct one to use will depend on what the protocol you're using dictates.

Comments

1

That's because i exists only within the for loop. Try adding all the values into an array and then access a specific item from it.

In your case, since you want to create a string in your code you could use StringBuilder to create the string. For example:

StringBuilder a = new StringBuilder();
a.Append(Convert.ToChar(b[i]);
string str = a.ToString();

or in your example it would be:

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
StringBuilder a = new StringBuilder();

for (int i = 0; i < k; i++)
{
    a.Append(Convert.ToChar(b[i]);
}
string str = a.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.