Is there a better way to convert a ListArray into a byte array in C#? The ListArray is coming from a dictionary object and contains a ListArray of objects where the underlying type is int. The only way I could get it to work was by looping thru the ListArray and individually inserting into the byte array. I was trying to get it to work with ToArray but I kept getting a cast error.
static void Main(string[] args) { int intLoop; byte[] arrByte; string strJson = "{\"Data\":[104,101,108,108,111,32,119,111,114,108,100]}"; Dictionary<string, object> dic = new Dictionary<string, object>(); JavaScriptSerializer js = new JavaScriptSerializer(); //deserialize json into dictionary object dic = js.Deserialize<Dictionary<string, object>>(strJson); //convert arraylist into byte array intLoop = 0; arrByte = new byte[((System.Collections.ArrayList)dic["Data"]).Count]; foreach (var s in (System.Collections.ArrayList)dic["Data"]) { arrByte[intLoop] = Convert.ToByte(s); intLoop++; } }
Dataproperty of typeList<byte>. learn.microsoft.com/en-us/dotnet/api/…ArrayList? It really shouldn;t be used anymore