copy byte array

T

TonyJ

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.Encoding.ASCII.GetBytes("This is a good test
for me");
string s = System.Text.Encoding.UTF8.GetString(buffer);
byte[] newBuffer = new byte[30];
Array.Copy(buffer,3, newBuffer, 0, 10 );
s = System.Text.Encoding.UTF8.GetString(newBuffer);
 
C

Claes Bergefall

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

/claes
 
J

Jon Skeet [C# MVP]

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.GetString which takes an index and a
count as well as the byte array.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top