473,385 Members | 1,185 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,385 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 54190
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...
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: 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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.