Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an object contains a byte array?

Tags:

arrays

c#

I'm having an issue with the following code.

byte[] array = data as byte[]; // compile error - unable to use built-in conversion

if (array != null) { ...

I only want to assign the data to array variable if the data is actually a byte array.

like image 206
Stephen Price Avatar asked Jan 22 '10 03:01

Stephen Price


People also ask

How can you tell if a class is a byte array?

In order to determine if an object is an Object is an array in Java, we use the isArray() and getClass() methods. The getClass() method method returns the runtime class of an object.

How do I find the byte array of an image?

Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.

What does a byte array contain?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..

What data type is a byte array?

The bytearray type is a mutable sequence of integers in the range between 0 and 255. It allows you to work directly with binary data. It can be used to work with low-level data such as that inside of images or arriving directly from the network. Bytearray type inherits methods from both list and str types.


2 Answers

Try

if(data.GetType().Name == "Byte[]") 
{
    // assign to array
}
like image 103
Joel Etherton Avatar answered Oct 09 '22 16:10

Joel Etherton


How about this:

byte[] array = new  byte[arrayLength];
if (array is byte[])
{
    // Your code
}
like image 36
Upul Bandara Avatar answered Oct 09 '22 16:10

Upul Bandara