473,748 Members | 5,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting Object to byte array

I have a COM server that is returning a type Object that I need to cast as a
byte array. Can anyone tell me how this is performed in C#?

Thanks,

glenn
Nov 17 '05 #1
3 54268
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.

glenn
"glenn" <gh******@softe ksoftware.com> wrote in message
news:OR******** ******@TK2MSFTN GP09.phx.gbl...
I have a COM server that is returning a type Object that I need to cast as a byte array. Can anyone tell me how this is performed in C#?

Thanks,

glenn

Nov 17 '05 #2
glenn <gh******@softe ksoftware.com> wrote:
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.


That's fine, except you're creating a new byte array for no reason -
just generating garbage.

Just use:

byte[] b = (byte[]) myobj;

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
thanks

glenn

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
glenn <gh******@softe ksoftware.com> wrote:
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was getting errors.


That's fine, except you're creating a new byte array for no reason -
just generating garbage.

Just use:

byte[] b = (byte[]) myobj;

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4

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

Similar topics

2
26914
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an IPv4 address. Ok, yes, in theory, there are enough bits to hold the values. But, my Java book clearly states that a byte is a SIGNED quantity, is part of the Integer class, and can hold values ranging from 127
1
1070
by: AndyM | last post by:
I am trying to figure out how to serialize data over a socket in C#. Every example I have seen shows sending a string by converting it to a byte array and then passing to Send(). Simple enough. But how do I convert longs/shorts etc. to a byte array? How about structs? I figure I need to somehow serialize it to a stream or something but I'm a little lost. When I did this stuff in C++ I would just cast the struct to a void * and pass it. ...
6
3392
by: Joe Thompson | last post by:
Using VS.Net 2003 - a managed VC++ WinForms app: If I have an unmanged structure like typedef struct { unsigned char Field1; unsigned char Field2; unsigned int Field3: 6; unsigned int Field4: 2;
3
1550
by: Fireangel | last post by:
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...
4
3194
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can get public members but not protected, private, static members"
20
3522
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int *)a2; // now a1=a2, a1=a2, etc.
12
3336
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /* TestA.cs */ namespace TEST { class TestA { public ushort val1; public ushort val2; public ushort val3;
10
6377
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...
0
8830
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
9544
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
9247
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...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
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...
1
3313
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
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.