Convert byte[] into Array?

P

Petar Popara

I need some help converting byte[] into Array:

int bufferLen = Convert.ToInt32(file.Length);
byte buffer = new byte[bufferLen];
int len = sr.Read(buffer, 0, bufferLen);
ArrayList a = new ArrayList(len);
a.Add(buffer);

Array result = a.toArray();

Something like that...
 
J

Johann Blake

Peter,

An Array class is not the same thing as an ArrayList. When you add an
item to ArrayList, you can only access it by accessing its index. So I
believe you are looking for something like this:

byte[] byteArray = new byte[1];
byteArray[0] = 1;

ArrayList arrayList = new ArrayList(byteArray.Length);
arrayList.Add(byteArray);

byte[] someByteArray = (byte[]) arrayList[0];

Best Regards
Johann Blake
 
P

Petar Popara

So I
believe you are looking for something like this:

No. :(

I need Array class, because I'm using it in COM automation (SafeArray).

Something like this:

int bufferLen = Convert.ToInt32(file.Length);
char[] buffer = new char[bufferLen];
int len = sr.Read(buffer, 0, bufferLen);
byte[] tmp = new byte[0]; //Need VT_UI1 automation type. Is this OK?
Array a = Array.CreateInstance(tmp.GetType(), len);
a.SetValue(buffer);
 
W

William Stacey [MVP]

Does this work for you?
byte[] ba = new byte[2];
Array ar = (Array)ba;

--
William Stacey [MVP]

Petar Popara said:
So I
believe you are looking for something like this:

No. :(

I need Array class, because I'm using it in COM automation (SafeArray).

Something like this:

int bufferLen = Convert.ToInt32(file.Length);
char[] buffer = new char[bufferLen];
int len = sr.Read(buffer, 0, bufferLen);
byte[] tmp = new byte[0]; //Need VT_UI1 automation type. Is this OK?
Array a = Array.CreateInstance(tmp.GetType(), len);
a.SetValue(buffer);
 
P

Petar Popara

Does this work for you?

Yes. :)

I'm beginner with C# and even trivial things confuses me.

Is "byte" same as "unsigned char" (BYTE) in C/C++? I need VT_UI1 automation
(compatible) type.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


What you want to do ?

your code should work changing these lines like
byte buffer = new byte[bufferLen];

a.Add(buffer);
for
byte[] buffer = new byte[bufferLen];


a.AddRange( buffer )


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top