How to get a Byte Array ?

Using (or providing) Microsoft.NET Classes

Re: How to get a Byte Array ?

Postby RossHale on Mon Aug 16, 2010 9:58 pm

⍝ My previous post got snarled at the end, so I am closing here.

⍝ My point is that you can create a .Net array such as System.Byte[]
⍝ by using the System.Array.CreateInstance method.

⍝ You can use the facilities of System.Collections.ArrayList
⍝ for more pleasant accessing and assignment of values in APL style.
⍝ The ArrayList.Adapter method can wrap a typed System.Array object.
⍝ An ArrayList.ToArray (or wrapper) can produce APL arrays with values derived from (possibly) converted System.Type(s).
⍝ Sometimes you have to use Convert.To(type) to assign conversions to the ArrayList before invoking the ToArray method.
⍝ You can use the ArrayList.CopyTo to copy and convert values to an array created by Array.CreateInstance.

⍝ Remember that ArrayList.ToArray yields an APL array of derivied values
⍝ which is a different creature from say .Net array such as System.Byte[].

Regards,
Ross
User avatar
RossHale
 
Posts: 31
Joined: Thu Jun 18, 2009 9:08 pm
Location: Bellevue, Washington, USA

Re: How to get a Byte Array ?

Postby PGilbert on Tue Aug 17, 2010 12:58 am

Thanks for your previous reply Ross, this is pure madness what you are showing us ! Now its possible to create .Net array and access it in a way similar to APL ! I was told some time ago that I could access the .Net world in a scalar way only (one item at a time).

Here is what works for me for an Int32:

      ba←System.Array.CreateInstance (System.Type.GetType ⊂'System.Int32') 100
      ba
System.Int32[]

      bax←System.Collections.ArrayList.Adapter ba
      bax
System.Collections.ArrayList+IListWrapper

      bax[⍳10]←100+⍳10
      bax[⍳10]
101 102 103 104 105 106 107 108 109 110

If I try the same thing for a System.Byte it works until I try to populate the array. We will wait for the answer of Vince about how to set the value of a Byte array, I assume we have the same issue here.

Many thanks,

Pierre Gilbert
User avatar
PGilbert
 
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to get a Byte Array ?

Postby Morten|Dyalog on Tue Aug 17, 2010 11:49 am

Code: Select all
aByteArray.SetValue 50 1

I believe this fails because SetValue takes System.Object as the first element. Dyalog APL thinks "System.Int32" will satisfy that, but then when SetValue tries to put the value into the array it discovers that it needed Byte (but failed to declare that).

((Convert.ToByte 50) 1) doesn't help because APL converts it straight back into an integer and strands it with 1. APL is perhaps being too helpful, thinking that since you are an APLer you probably want to do math on it. It might well be more useful if we did not do this automagically for Byte. An alternative would be to come up with some kind of "cast operator" which would allow:

Code: Select all
(aBA.SetValue {cast} Byte Int16) 50 1

We'll discuss this internally when John Daintree is back from vacation next week. Meanwhile, unless you can find a way to avoid using Byte arrays as temporary storage, I think you will need some kind of C# helper function. I'd be happy to help define this if you let me know more about the underlying problem that you are trying to solve.
User avatar
Morten|Dyalog
 
Posts: 460
Joined: Tue Sep 09, 2008 3:52 pm

Re: How to get a Byte Array ?

Postby OSRK on Sun Sep 08, 2024 5:58 pm

Are there any news with a way to make a .NET byte array from an APL array?
User avatar
OSRK
 
Posts: 17
Joined: Tue Dec 27, 2016 8:48 am

Re: How to get a Byte Array ?

Postby PGilbert on Mon Sep 09, 2024 12:02 pm

Here is what I am using to make a byte array:

      ba←ByteArray bytes;⎕IO
⍝ Function to get a .Net byte array
⍝ bytes = numbers in the range of 0 to 255
⍝ ba = .Net Byte[]

⎕USING∪←⊂'System,mscorlib.dll' ⋄ ⎕IO←0
ba←Array.CreateInstance(Byte(⍴,bytes)) ⍝ Empty array of type 'Byte'
{ba.Set ⍵}¨↓(⍳⍴,bytes),[0.5]bytes ⍝ Populate the Byte array
User avatar
PGilbert
 
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to get a Byte Array ?

Postby RichardP|Dyalog on Tue Sep 10, 2024 9:26 am

It appears you can also populate with square bracket indexing

Code: Select all
 ByteArray←{
     ⎕IO←0 ⋄ ⎕USING←''
     ba←System.Array.CreateInstance(System.Byte)(≢⍵)
     ba[⍳≢⍵]←⍵
     ba
 }
RichardP|Dyalog
 
Posts: 33
Joined: Fri Oct 12, 2018 3:05 pm

Previous

Return to Microsoft.NET

Who is online

Users browsing this forum: No registered users and 1 guest