469,934 Members | 2,132 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Assigning Data to byte[]

I'm looking to populate a byte array of some fixed size to send out over
a UDP connection. The data in the byte array is mixed between characters
and binary.

I'm a beginner to this language.

I can assign the character data by the byte. However, I'm not sure how I
would
assign a binary value to a certain location in a byte array where the value is
greater than 255. I have an unsigned short and want to assign it to a
specific
location in the byte array.

What is the best way to do this?

thanks!
Nov 16 '05 #1
4 10050
TRW1313,
I would consider using either a System.IO.BinaryWriter or the methods on
System.BitConverter along with Array.Copy or Buffer.BlockCopy.

Hope this helps
Jay
"TRW1313" <TR*****@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
I'm looking to populate a byte array of some fixed size to send out over
a UDP connection. The data in the byte array is mixed between characters
and binary.

I'm a beginner to this language.

I can assign the character data by the byte. However, I'm not sure how I
would
assign a binary value to a certain location in a byte array where the
value is
greater than 255. I have an unsigned short and want to assign it to a
specific
location in the byte array.

What is the best way to do this?

thanks!

Nov 16 '05 #2
given "private data[] as byte;"

....
data[158] = value;
....
would set the 159th ([0] is the 1st) element of the array to the value of
the variable "value".

Look also at the Convert.ToXXX() functions (in the Convert class) to convert
many things from/to a byte or byte array.

--
-Philip Rieck
http://philiprieck.com/blog/

-
"TRW1313" <TR*****@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
I'm looking to populate a byte array of some fixed size to send out over
a UDP connection. The data in the byte array is mixed between characters
and binary.

I'm a beginner to this language.

I can assign the character data by the byte. However, I'm not sure how I
would
assign a binary value to a certain location in a byte array where the
value is
greater than 255. I have an unsigned short and want to assign it to a
specific
location in the byte array.

What is the best way to do this?

thanks!

Nov 16 '05 #3
TRW
Thanks Jay for the help. I will look those over.

I thought the BitConverter would get the data out of the byte array for me but
not put it. I will recheck.

thanks again!

"Jay B. Harlow [MVP - Outlook]" wrote:
TRW1313,
I would consider using either a System.IO.BinaryWriter or the methods on
System.BitConverter along with Array.Copy or Buffer.BlockCopy.

Hope this helps
Jay
"TRW1313" <TR*****@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
I'm looking to populate a byte array of some fixed size to send out over
a UDP connection. The data in the byte array is mixed between characters
and binary.

I'm a beginner to this language.

I can assign the character data by the byte. However, I'm not sure how I
would
assign a binary value to a certain location in a byte array where the
value is
greater than 255. I have an unsigned short and want to assign it to a
specific
location in the byte array.

What is the best way to do this?

thanks!


Nov 16 '05 #4
TRW,
BitConverter can go both ways.

For example BitConverter.ToUInt16 will take the array & convert it to an
unsigned short.

While BitConverter.GetBytes(System.UInt16) will take an unsigned short &
convert it to a byte array.

Hence I would use BitConverter.GetBytes to convert the unsigned short to a
byte array, then use Array.Copy or Buffer.BlockCopy to "put" this byte array
into the larger byte array.

I normally prefer BinaryWriter (possibly over a MemoryStream) as it
encapsulates the above nicely.

Hope this helps
Jay

"TRW" <TR*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Thanks Jay for the help. I will look those over.

I thought the BitConverter would get the data out of the byte array for me
but
not put it. I will recheck.

thanks again!

"Jay B. Harlow [MVP - Outlook]" wrote:
TRW1313,
I would consider using either a System.IO.BinaryWriter or the methods on
System.BitConverter along with Array.Copy or Buffer.BlockCopy.

Hope this helps
Jay
"TRW1313" <TR*****@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
> I'm looking to populate a byte array of some fixed size to send out
> over
> a UDP connection. The data in the byte array is mixed between
> characters
> and binary.
>
> I'm a beginner to this language.
>
> I can assign the character data by the byte. However, I'm not sure
> how I
> would
> assign a binary value to a certain location in a byte array where the
> value is
> greater than 255. I have an unsigned short and want to assign it to a
> specific
> location in the byte array.
>
> What is the best way to do this?
>
> thanks!


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Eric Bantock | last post: by
reply views Thread by NicK chlam via DotNetMonster.com | last post: by
5 posts views Thread by DC Gringo | last post: by
9 posts views Thread by Thiru .Net | last post: by
reply views Thread by Macca | last post: by
3 posts views Thread by Chris Saunders | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.