Convert byte[] into Array?

  • Thread starter Thread starter Petar Popara
  • Start date Start date
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...
 
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
 
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);
 
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);
 
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.
 
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,
 
Back
Top