5 Replies - 256 Views - Last Post: 28 January 2013 - 10:24 AM Rate Topic: -----

#1 jack061989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 13

For Loop and Byte Reading

Posted 28 January 2013 - 09:38 AM

Hi im currently pursuing engineering and im learning C sharp as it was widely used for framework projects...
Im trying to write a program and im stuck with the following:-

scenario:
byte msg[]
for(i=0;i<12;i++)
{
string msg1;
msg1= msg[i].ToString();
Assert.Areequal("some statement",the message is,msg1,0)
}



The output is coming like this:-
0
0
0
1
0
0
0
0
12
32
24
255


i.e im reading every byte of the message i.e there are totally twelve bytes..
i need to get every byte mentioned above in multiple strings for eg byte 1 in msg1 , byte 2 in msg2 , byte 3 in msg3 like that..but i cant able to get the bytes..for eg if i need to get the byte 1 alone to be printed i cant figure out to do that..Please guide me..thanks.......
PS :- similarly whether it is possible to print the complete message together for eg 00010000123224255 in a single instance

This post has been edited by Skydiver: 28 January 2013 - 09:42 AM
Reason for edit:: Put code in code tags. Please learn to do it yourself next time.


Is This A Good Question/Topic? 0
  • +

Replies To: For Loop and Byte Reading

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3161
  • View blog
  • Posts: 9,541
  • Joined: 05-May 12

Re: For Loop and Byte Reading

Posted 28 January 2013 - 09:46 AM

I doubt that your code above even compiles.

Anyway, in response to what you need, if you have an array of bytes, you could also have an array or list of strings. I recommend going with the list.

But sounds like you don't even need the string. If you just need a particular byte to be printed out, what is wrong with:
Console.WriteLine(msg[i]);    // i is the index of the byte to print out


Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: For Loop and Byte Reading

Posted 28 January 2013 - 09:46 AM

Quote

The output is coming like this:-

Not from that code it isn't. That code won't compile.

Please copy/paste the actual code from Visual Studio.

Retyping code makes it prone to transcription errors like we see here making it - well, not the actual code you need help with.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: For Loop and Byte Reading

Posted 28 January 2013 - 09:53 AM

You're saying 'byte' but I think you really mean 'character'.

First of all, strings are already a char[]
string demo = "yogi bear";
// means
// demo[0] == y
// demo[1] == o
// demo[2] == g
// demo[3] == i


if you want each character of a string you can access them directly by index.

Also you can call .ToString() on the byte[]

Console.WriteLine(msg1.ToString() );

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3161
  • View blog
  • Posts: 9,541
  • Joined: 05-May 12

Re: For Loop and Byte Reading

Posted 28 January 2013 - 09:56 AM

Actually, in .NET, each char is 2 bytes, because each char is 16-bits for UTF-16 encoding of strings, but everything else written by tlhIn`toq above about accessing characters is true.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: For Loop and Byte Reading

Posted 28 January 2013 - 10:24 AM

The byte[] has to come from someplace. As he is an engineering student and using a byte[] its not inconceivable that the source is a COM port. So the values could be ASCII - so one byte one character.

The real trick though is if the byte is 1 they want to display the number 1 - so will will need to be translated. Using ASCII would probably make it easier: Just add 48 to the byte value

0 becomes 48 which is ASCII "0"
1 becomes 49 which is ASCII "1"

http://www.theasciicode.com.ar/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1