473,760 Members | 9,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Byte Array



I want to cast a class into a byte array. I've seen some examples of this
floating around, but they all have simple data members. What happens if I
cast something that has a ArrayList or an Array?? Does it loop through the
array and cast them all and I end up with a big array (This is what I hope
will happen)? Or does it simple cast the pointer address and I end up with a
small array??

Related question: How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast?? And if I
try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..

GE
Nov 17 '05 #1
3 1550
> I want to cast a class into a byte array. I've seen some examples of this
floating around, but they all have simple data members. What happens if I
cast something that has a ArrayList or an Array?? Does it loop through the array and cast them all and I end up with a big array (This is what I hope
will happen)? Or does it simple cast the pointer address and I end up with a small array??

Related question: How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast?? And if I try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..


Are we talking about managed ref types here? If so, you should *never* do
this, you're won't get what you expect, and you're likely to end up
corrupting the managed heap if you manipulate it this way.

Why do you need to do this? If you're trying to call into native code
passing this, then there are appropriate methods for this (P/Invoke,
StructLayout, etc).

If you're trying, for example, to save the contents of the class to disk or
something like that, then using Serialization and the Serialization
formatters (e.g. BinaryFormatter ) are the way to go.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2

I'm using only managed types.

In a nut shell, I'm trying to send whole classes across the network using
the socket class. The socket class only sends and received using Byte Arrays
(__gc array). I *THINK* i've seen some examples of ppl casting a class into
a byte array, sending it, then casting it back into the class on the other
end. I say I think because I was looking for something else when I came
across it, and it was a fair amount ago.

I have to cast into a byte array. I have no choice. I do want to make sure
i get back what I want though. And If I can avoid having to write a whole
bunch of functions to create a byte array, I would be very happy :)

GE

"Tomas Restrepo (MVP)" wrote:
I want to cast a class into a byte array. I've seen some examples of this
floating around, but they all have simple data members. What happens if I
cast something that has a ArrayList or an Array?? Does it loop through

the
array and cast them all and I end up with a big array (This is what I hope
will happen)? Or does it simple cast the pointer address and I end up

with a
small array??

Related question: How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast?? And if

I
try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..


Are we talking about managed ref types here? If so, you should *never* do
this, you're won't get what you expect, and you're likely to end up
corrupting the managed heap if you manipulate it this way.

Why do you need to do this? If you're trying to call into native code
passing this, then there are appropriate methods for this (P/Invoke,
StructLayout, etc).

If you're trying, for example, to save the contents of the class to disk or
something like that, then using Serialization and the Serialization
formatters (e.g. BinaryFormatter ) are the way to go.

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #3
Fireangel,
I'm using only managed types.

In a nut shell, I'm trying to send whole classes across the network using
the socket class. The socket class only sends and received using Byte Arrays (__gc array). I *THINK* i've seen some examples of ppl casting a class into a byte array, sending it, then casting it back into the class on the other
end. I say I think because I was looking for something else when I came
across it, and it was a fair amount ago.
That's common unmanaged C++ practice, but won't really do the trick in
managed code.

I have to cast into a byte array. I have no choice. I do want to make sure i get back what I want though. And If I can avoid having to write a whole
bunch of functions to create a byte array, I would be very happy :)


Serialization is your friend. Basically, all you'd need to do is mark your
managed types as [ Serializable ] and use the BinaryFormatter class to
serialize it into a stream and deserialize them from a string.

There are plenty of examples of this on the net...

That said, since it seems you have control of both client and server, why
invent your own protocol? Just use .NET remoting and be done with it....

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4

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

Similar topics

16
14136
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
15
34602
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
8
10718
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
8
4592
by: frekster | last post by:
Hi. I used to be able to do this easily in vb 6 via looping and preserving the source array data/size etc. How can I do this in vb.net? I've been trying for a while now and this should be an easy task but it just isn't clicking. For some reason, the preserve doesn't seem to be working.
5
9842
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy the memory across. Using Marshal.PtrStructure doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it). ...
6
2783
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
17
7253
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
24
2292
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
10
6379
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
2
7207
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
9521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
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,...
0
10107
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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,...
1
7324
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
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
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
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.