byte problem

G

Guest

hi,

im a newbie here and in desperate need of help. Some of you might think this
is a stupid question or a too easy to answer question but here goes. I am
making a UDP client server application using C# everything is all set except
for one thing. when sending a message, the message should contain 2 byte for
squence number another 2 bytes for lenght and the rest is for data or the
message. my problem is i dont know how to code the needed 2 bytes.. i mean
how do i code 2 bytes or can anyone help me with the concept of using this
bytes and how do i declare 2 bytes and use it? i really cant understand...
thank you so much! i would really appreciate any help at all...
 
G

Guest

Hi,

The code below shows how to convert a short value to array of 2 bytes. The
same class "BitConverter" can be used to convert any numeric value to byte
array and vice-versa.

short sequenceNumber = 5;
short msgLength = 12000;

byte[] sequenceNumberBytes = BitConverter.GetBytes(sequenceNumber);
// The length of the byte array will be 2
Console.WriteLine(sequenceNumberBytes.Length);

byte[] msgLengthBytes = BitConverter.GetBytes(msgLength);
// The length of the byte array will be 2
Console.WriteLine(msgLengthBytes.Length); // This will print 2

Hope this helps.
 
G

Guest

Thanks Adityanand.. It was very helpful. Thank you so much for the help!!!!

Adityanand Pasumarthi said:
Hi,

The code below shows how to convert a short value to array of 2 bytes. The
same class "BitConverter" can be used to convert any numeric value to byte
array and vice-versa.

short sequenceNumber = 5;
short msgLength = 12000;

byte[] sequenceNumberBytes = BitConverter.GetBytes(sequenceNumber);
// The length of the byte array will be 2
Console.WriteLine(sequenceNumberBytes.Length);

byte[] msgLengthBytes = BitConverter.GetBytes(msgLength);
// The length of the byte array will be 2
Console.WriteLine(msgLengthBytes.Length); // This will print 2

Hope this helps.

--
Regards,
Aditya.P


Rain said:
hi,

im a newbie here and in desperate need of help. Some of you might think this
is a stupid question or a too easy to answer question but here goes. I am
making a UDP client server application using C# everything is all set except
for one thing. when sending a message, the message should contain 2 byte for
squence number another 2 bytes for lenght and the rest is for data or the
message. my problem is i dont know how to code the needed 2 bytes.. i mean
how do i code 2 bytes or can anyone help me with the concept of using this
bytes and how do i declare 2 bytes and use it? i really cant understand...
thank you so much! i would really appreciate any help at all...
 
G

Guest

if i have a string, how do i get the first 2 bytes of it? thanks

Adityanand Pasumarthi said:
Hi,

The code below shows how to convert a short value to array of 2 bytes. The
same class "BitConverter" can be used to convert any numeric value to byte
array and vice-versa.

short sequenceNumber = 5;
short msgLength = 12000;

byte[] sequenceNumberBytes = BitConverter.GetBytes(sequenceNumber);
// The length of the byte array will be 2
Console.WriteLine(sequenceNumberBytes.Length);

byte[] msgLengthBytes = BitConverter.GetBytes(msgLength);
// The length of the byte array will be 2
Console.WriteLine(msgLengthBytes.Length); // This will print 2

Hope this helps.

--
Regards,
Aditya.P


Rain said:
hi,

im a newbie here and in desperate need of help. Some of you might think this
is a stupid question or a too easy to answer question but here goes. I am
making a UDP client server application using C# everything is all set except
for one thing. when sending a message, the message should contain 2 byte for
squence number another 2 bytes for lenght and the rest is for data or the
message. my problem is i dont know how to code the needed 2 bytes.. i mean
how do i code 2 bytes or can anyone help me with the concept of using this
bytes and how do i declare 2 bytes and use it? i really cant understand...
thank you so much! i would really appreciate any help at all...
 
J

Jon Skeet [C# MVP]

Rain said:
if i have a string, how do i get the first 2 bytes of it? thanks

Strings are sequences of *characters*, not bytes. If you have arbitrary
binary data in your string at the start, you're asking for trouble.

How did you read the string to start with? You might find it better to
read two bytes and then read the string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top