Storing a word inside a Byte array

  • Thread starter Thread starter artist
  • Start date Start date
A

artist

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!
 
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)
 
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];



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)
 
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 said:
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];



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)
 
artist said:
parez said:
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
 
Doug said:
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
 
Back
Top