473,804 Members | 3,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3409
On Aug 7, 1:50*pm, artist <nos...@spam.co mwrote:
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.Ge tBytes(aaa)
Aug 7 '08 #2
Is there not an easier way? In C++ you just need to do this:

*((WORD*)msgDat a) = aaa;

If I use BitConverter I have to do all this:

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


parez wrote:
On Aug 7, 1:50 pm, artist <nos...@spam.co mwrote:
>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.Ge tBytes(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.co mwrote in message
news:Om******** *******@TK2MSFT NGP05.phx.gbl.. .
Is there not an easier way? In C++ you just need to do this:

*((WORD*)msgDat a) = aaa;

If I use BitConverter I have to do all this:

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


parez wrote:
>On Aug 7, 1:50 pm, artist <nos...@spam.co mwrote:
>>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.Ge tBytes(aaa)
Aug 7 '08 #4
artist wrote:
parez wrote:
>On Aug 7, 1:50 pm, artist <nos...@spam.co mwrote:
>>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.Ge tBytes(aaa)
Is there not an easier way? In C++ you just need to do this:

*((WORD*)msgDat a) = 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.Ge tBytes(aaa);
msgData[0] = aSize[0];
msgData[1] = aSize[1];
Or:

byte[] aSize = new byte[2];
BitConverter.Ge tBytes(aaa).Cop yTo(aSize, 0);

Or:

byte[] aSize = new byte[2];
Array.Copy(BitC onverter.GetByt es(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
7927
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" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language...
15
2343
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
2876
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 o1,o2,o3, range 0 .. 2^8-1 How to do this in a clever way? (The 6 bits integers represent characters in range A-Z and 0-9)
2
3413
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 file, convert the FileStream into a string using Unicode encoding, then do a replace, then convert the string back to a byte using Unicode encoding which i then Response.WriteBinary(bytes) to the browser. This works fine although the actual...
6
3204
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 me like there were performance issues at the time. How about the different types? The MS docs I would expect Access to differentiate and handle appropriately (i.e. .DOC and .XLS).. but how about ..PDF? and can I stash a .TXT document in the...
2
7855
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 framework 1.1). I've successfully loaded the document into a XmlDocument DOM parser (I can dump the contents using OuterXML). And, I've created a XmlNamespaceManager and assigned it the "w" namespace.
5
2870
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 wrote this method for getting a password from the console and converting it to an array of bytes for later use in the encryption algorithm. The weak point as I see it is the storage of the password - it will be
8
2125
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 command word) which might be int or double or char (again defined by the command word). For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might be followed by the command byte STRING would be followed by a...
20
2825
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 string VB.NET data type, knowing that this is its equivalence (for char4)? Next is the data type definition '''unsigned char
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.