How to get a Byte Array ?
16 posts
• Page 2 of 2 • 1, 2
Re: How to get a Byte Array ?
⍝ 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
⍝ 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
-
RossHale - Posts: 31
- Joined: Thu Jun 18, 2009 9:08 pm
- Location: Bellevue, Washington, USA
Re: How to get a Byte Array ?
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
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
-
PGilbert - Posts: 440
- Joined: Sun Dec 13, 2009 8:46 pm
- Location: Montréal, Québec, Canada
Re: How to get a Byte Array ?
- 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.
-
Morten|Dyalog - Posts: 460
- Joined: Tue Sep 09, 2008 3:52 pm
Re: How to get a Byte Array ?
Are there any news with a way to make a .NET byte array from an APL array?
-
OSRK - Posts: 17
- Joined: Tue Dec 27, 2016 8:48 am
Re: How to get a Byte Array ?
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
-
PGilbert - Posts: 440
- Joined: Sun Dec 13, 2009 8:46 pm
- Location: Montréal, Québec, Canada
Re: How to get a Byte Array ?
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
16 posts
• Page 2 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group