-5

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();
    }
Nichole Grace
  • 213
  • 1
  • 2
  • 11
  • 1
    Why are you using `ArrayList` ? It seems that your array list has an array of bytes. See: [How convert byte array to string](http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string) – Habib Sep 04 '14 at 19:55
  • 2
    `String TEST = Encoding.UTF8.GetString((byte[])ArrayList[i]);` – L.B Sep 04 '14 at 19:56

2 Answers2

4

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]);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

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.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103