...bringing you notes from the field...

Tag Cloud


Why do I get: System.Byte[] for a String?

There are several circumstances which will display this output (or exception in some cases) in .NET, however the primary reason is because there is a mismatch between the expected format and the casting taking place in your application.  An example would be attributes which are bound to LDAP that do not readily cast to string.

Here is a quick solution (that has its roots in vc++) for successfully casting this datatype to string:

byte[] myByteArray = (byte[])result.Properties["memberOf"][counter];
string myString = "";
foreach (byte b in myByteArray)
{
    char singleChar = Convert.ToChar(b);
    myString += singleChar.ToString();
}

I just received some feedback from Joe. He was kind enough to offer alternative code:

 

                  byte [] bytes = Encoding.ASCII.GetBytes("This is a test");
                  String s = Encoding.ASCII.GetString(bytes);

 


  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Why do I get: System.Byte[] for a String?

perhaps see post as well

http://techrookies.blogspot.com/2008/07/when-databinding-results-in-systembyte.html 7/22/2008 2:20 AM | Hamy