473,325 Members | 2,860 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,325 software developers and data experts.

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 54168
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******@softeksoftware.com> wrote in message
news:OR**************@TK2MSFTNGP09.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******@softeksoftware.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
glenn <gh******@softeksoftware.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.com>
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
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...
1
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...
6
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...
3
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...
4
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...
20
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...
12
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. /*...
10
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.