473,387 Members | 1,440 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,387 software developers and data experts.

Storing a word inside a Byte array

Hi all,

I have the following:

byte[] msgData = new byte[100];
ushort aaa = 0xFFFA;

Now I would like to store that variable aaa in the array
so at the end I would have something like:
msgData[0] = 0xFF;
msgData[1] = 0xFA;

Since I cannot do something like this:
m_buffer[0] = aaa;

Is there an easy and fast way of doing that?
I know i can extract the first byte from aaa and put it in msgData[0]
and the same with the second byte, but I was wondering if there is
something simpler than that.

I'm afraid this is a trivial question, but I haven't been able
to find the answer.

Thanks!
Aug 7 '08 #1
6 3379
On Aug 7, 1:50*pm, artist <nos...@spam.comwrote:
Hi all,

I have the following:

byte[] msgData = new byte[100];
ushort aaa = 0xFFFA;

Now I would like to store that variable aaa in the array
so at the end I would have something like:
msgData[0] = 0xFF;
msgData[1] = 0xFA;

Since I cannot do something like this:
m_buffer[0] = aaa;

Is there an easy and fast way of doing that?
I know i can extract the first byte from aaa and put it in msgData[0]
and the same with the second byte, but I was wondering if there is
something simpler than that.

I'm afraid this is a trivial question, but I haven't been able
to find the answer.

Thanks!
try BitConverter.GetBytes(aaa)
Aug 7 '08 #2
Is there not an easier way? In C++ you just need to do this:

*((WORD*)msgData) = aaa;

If I use BitConverter I have to do all this:

byte[] aSize = new byte[2];
aSize = BitConverter.GetBytes(aaa);
msgData[0] = aSize[0];
msgData[1] = aSize[1];


parez wrote:
On Aug 7, 1:50 pm, artist <nos...@spam.comwrote:
>Hi all,

I have the following:

byte[] msgData = new byte[100];
ushort aaa = 0xFFFA;

Now I would like to store that variable aaa in the array
so at the end I would have something like:
msgData[0] = 0xFF;
msgData[1] = 0xFA;

Since I cannot do something like this:
m_buffer[0] = aaa;

Is there an easy and fast way of doing that?
I know i can extract the first byte from aaa and put it in msgData[0]
and the same with the second byte, but I was wondering if there is
something simpler than that.

I'm afraid this is a trivial question, but I haven't been able
to find the answer.

Thanks!

try BitConverter.GetBytes(aaa)
Aug 7 '08 #3
Well you haven't told us what you are trying to do, but if you are trying to
interface data with an unmanaged app and want to stream out more than just a
single variable then you might like to look at BinaryWriter.

Cheers
Doug Forster

"artist" <no****@spam.comwrote in message
news:Om***************@TK2MSFTNGP05.phx.gbl...
Is there not an easier way? In C++ you just need to do this:

*((WORD*)msgData) = aaa;

If I use BitConverter I have to do all this:

byte[] aSize = new byte[2];
aSize = BitConverter.GetBytes(aaa);
msgData[0] = aSize[0];
msgData[1] = aSize[1];


parez wrote:
>On Aug 7, 1:50 pm, artist <nos...@spam.comwrote:
>>Hi all,

I have the following:

byte[] msgData = new byte[100];
ushort aaa = 0xFFFA;

Now I would like to store that variable aaa in the array
so at the end I would have something like:
msgData[0] = 0xFF;
msgData[1] = 0xFA;

Since I cannot do something like this:
m_buffer[0] = aaa;

Is there an easy and fast way of doing that?
I know i can extract the first byte from aaa and put it in msgData[0]
and the same with the second byte, but I was wondering if there is
something simpler than that.

I'm afraid this is a trivial question, but I haven't been able
to find the answer.

Thanks!

try BitConverter.GetBytes(aaa)
Aug 7 '08 #4
artist wrote:
parez wrote:
>On Aug 7, 1:50 pm, artist <nos...@spam.comwrote:
>>byte[] msgData = new byte[100];
ushort aaa = 0xFFFA;

Now I would like to store that variable aaa in the array
so at the end I would have something like:
msgData[0] = 0xFF;
msgData[1] = 0xFA;

Since I cannot do something like this:
m_buffer[0] = aaa;

Is there an easy and fast way of doing that?
I know i can extract the first byte from aaa and put it in msgData[0]
and the same with the second byte, but I was wondering if there is
something simpler than that.
try BitConverter.GetBytes(aaa)
Is there not an easier way? In C++ you just need to do this:

*((WORD*)msgData) = aaa;
Unless you happen to code on Solaris (or to be one of the
language fanatics at clc).

In that case you will be forces to use memcpy.
If I use BitConverter I have to do all this:

byte[] aSize = new byte[2];
aSize = BitConverter.GetBytes(aaa);
msgData[0] = aSize[0];
msgData[1] = aSize[1];
Or:

byte[] aSize = new byte[2];
BitConverter.GetBytes(aaa).CopyTo(aSize, 0);

Or:

byte[] aSize = new byte[2];
Array.Copy(BitConverter.GetBytes(aaa), aSize, 2):

Arne


Aug 8 '08 #5
Doug Forster wrote:
Well you haven't told us what you are trying to do, but if you are
trying to interface data with an unmanaged app and want to stream out
more than just a single variable then you might like to look at
BinaryWriter.
Even without then a BinaryWriter around a MemoryStream could
be an option.

Arne
Aug 8 '08 #6
Thanks. It looks good.
Aug 8 '08 #7

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

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
15
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
19
by: becte | last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8) like this 11111122|22223333|33444444 Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1 and the output is int...
2
by: Christopher Beltran | last post by:
I am currently trying to replace certain strings, not single characters, with other strings inside a word document which is then sent to a browser as a binary file. Right now, I read in the word...
6
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to...
2
by: Andy | last post by:
Hi, I have an XML document that uses namespaces (it is from a Word 2007 file). I want to retrieve all the "t" elements that belong to the "w" namespace (<w:t>) using XPath from VB.NET 2003 (.NET...
5
by: per9000 | last post by:
Hi all, I want to create an encryption program and started thinking about not storing sensitive information in the memory since I guess someone might steal my computer an scan my memory. So I...
8
by: Jay | last post by:
I'm trying to store a sequence of operations and values of different types into a single array. It's a sequence of command word bytes, and a sequence of one or more values (as determined by the...
20
by: =?Utf-8?B?ZW1pdG9qbGV5ZXM=?= | last post by:
Hi everyone: i read from the documentation of a dll that there is a struct that uses char for storing an IP address. How can it be? and how come i can get to representate the same value in a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.