You must Sign In to post a response.
  • Category: ASP.NET

    How to convert string to bytes

    Hi Developers,
    i got error in a line that is like below:
    Unable to cast object of type 'System.String' to type 'System.Byte[]'.
    My code :
    Response.BinaryWrite((byte[])dr["data"]);

    How am correct the error Anyone know please

    Thanks with
    PaulS
  • #762340
    Hi

    You can go through Below links for Convert string to bytes

    http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte

    http://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp

    http://net-informations.com/q/faq/stringtobyte.html

    http://www.codeproject.com/Questions/211192/How-to-convert-string-to-byte-array-and-vice-versa

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762358

    Hi Paul Raj,

    You asked the same-thing 2 days back also, what happened.? still you face the same problem have you check the null case before converting into byte format. Please check your previous post and let me know.


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762362
    Am sorrry Mr.naveen , you r right but i can't able to got exactly output. so i am remember.
    Thanking you
    Paul.S

  • #762364
    Hi Paul Raj,

    You can try this sample,


    byte[] data = System.Text.UTF8Encoding.GetBytes(Convert.ToString(dr["data"]));


    Try out the above syntax and let me know..

    Note : If the answer is really helpful to you then only make it as Best Answer, no need to make it Best Answer for all posts.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments