473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

byte problem

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...
Oct 18 '06 #1
4 1573
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" wrote:
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...
Oct 18 '06 #2
Thanks Adityanand.. It was very helpful. Thank you so much for the help!!!!

"Adityanand Pasumarthi" wrote:
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" wrote:
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...
Oct 18 '06 #3
if i have a string, how do i get the first 2 bytes of it? thanks

"Adityanand Pasumarthi" wrote:
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" wrote:
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...
Oct 19 '06 #4
Rain <Ra**@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
3
by: jackmejia | last post by:
Hello I am fighting to sync a C++ client with a C# server, I have managed to create a byte array in C++ stored as char* to be sent over the network to the server written in C#. on the C# side,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
3
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.