473,385 Members | 1,615 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.

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 10385
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
5
by: DC Gringo | last post by:
I am having a problem reading a simple update to the database. Basically I'm testing a small change to the pubs database -- changing the price of the Busy Executive's Database Guide from 19.99 to...
9
by: Thiru .Net | last post by:
how to assign unsigned values in vb.net i have a statement like this in c# public const uint FEEDER = 0x00000001; when i tried to convert above into vb.net i said Public Const FEEDER As...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
8
by: mast2as | last post by:
I almost apologize to ask this question but I have been starting at this code for a bit of time and don't understand what's wrong with it. I am not arguing about the fact it's good or not coding,...
4
jeffbroodwar
by: jeffbroodwar | last post by:
Hello, i have a problem about assigning a char value to a byte... please check the code below : ======================================================== Scenario # 1 : This code doesn't work : ...
3
by: Chris Saunders | last post by:
My C skills are rather meager so forgive me if I do not express my question clearly enough. Here is a struct that is declared in Windows: typedef struct _REPARSE_GUID_DATA_BUFFER { DWORD...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.