copy byte array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TonyJ

    copy byte array

    Hello!

    Now to my question.
    Here I copy from a byte array into another byte array and then convert to a
    string.
    Is it possible to convert some part from a byte array into a string directly
    without having
    to use a temporary byte array as I have done below?

    //byte[] buffer = new byte[30];
    byte[] buffer = System.Text.Enc oding.ASCII.Get Bytes("This is a good test
    for me");
    string s = System.Text.Enc oding.UTF8.GetS tring(buffer);
    byte[] newBuffer = new byte[30];
    Array.Copy(buff er,3, newBuffer, 0, 10 );
    s = System.Text.Enc oding.UTF8.GetS tring(newBuffer );


  • Jon Skeet [C# MVP]

    #2
    Re: copy byte array

    On Feb 15, 9:52 am, "TonyJ" <johansson.ande rs...@telia.com wrote:
    Now to my question.
    Here I copy from a byte array into another byte array and then convert to a
    string.
    Is it possible to convert some part from a byte array into a string directly
    without having to use a temporary byte array as I have done below?
    Yes. Use the overload of Encoding.GetStr ing which takes an index and a
    count as well as the byte array.

    Jon

    Comment

    • Claes Bergefall

      #3
      Re: copy byte array

      Check the overloaded version of GetString that takes an index and a count.

      /claes

      "TonyJ" <johansson.ande rsson@telia.com wrote in message
      news:uTDpOg7bIH A.288@TK2MSFTNG P02.phx.gbl...
      Hello!
      >
      Now to my question.
      Here I copy from a byte array into another byte array and then convert to
      a
      string.
      Is it possible to convert some part from a byte array into a string
      directly
      without having
      to use a temporary byte array as I have done below?
      >
      //byte[] buffer = new byte[30];
      byte[] buffer = System.Text.Enc oding.ASCII.Get Bytes("This is a good
      test
      for me");
      string s = System.Text.Enc oding.UTF8.GetS tring(buffer);
      byte[] newBuffer = new byte[30];
      Array.Copy(buff er,3, newBuffer, 0, 10 );
      s = System.Text.Enc oding.UTF8.GetS tring(newBuffer );
      >
      >

      Comment

      Working...