I am trying to copy first item of the array list to a string variable. I did the following but it returns System.Byte[].Please help
for(i=0; i<= ArrayList.Count;i++)
{
String TEST = ArrayList[i].ToString();
}
I am trying to copy first item of the array list to a string variable. I did the following but it returns System.Byte[].Please help
for(i=0; i<= ArrayList.Count;i++)
{
String TEST = ArrayList[i].ToString();
}
it returns System.Byte[]
The default behavior of .ToString()
just outputes the name of the type of the object. It is overridden in some types (like value types) to show some representation of the value of the object. Since ToString
is not overridden for arrays you are just seeing the type name.
There are multiple ways (ASCII, UTF8, Unicode) to convert a Byte[]
to a string, so you need to specify which one to use. If you want to use the default encoding for the system use
System.Text.Encoding.Default.GetString(ArrayList[i]);
It means, that the code it returning an object, which is System.Byte[]
array.
ArrayList[i].ToString(); // System.Byte[]
..no matter how many times you do it. It will always return to be the same.
Use this instead
using System.Text; // <-- add this
// inside the code
for(i = 0 ; i <= ArrayList.Count ; i++ )
{
string TEST = Encoding.UTF8.GetString(ArrayList[i]);
}
..this will encode the bytes to a string representation of the data.