Solved

converting StringBuilder to byte[]

Posted on 2008-10-17
10
6,587 Views
Last Modified: 2012-05-05
I am converting a StringBuilder to byte[].  Which is optimal from a boxing / unboxing perspective:

1. byte[] the_array = System.Text.Encoding.UTF8.GetBytes(the_stringbuilder.ToString());

2. byte[] the_array = System.Text.Encoding.UTF8.GetBytes(the_stringbuilder.ToCharArray());


0
Comment
Question by:gustierng
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 6

Expert Comment

by:mirzas
ID: 22739545
2.
0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22739606
both are optimal
because both are using the same procedure to follow.


0
 

Author Comment

by:gustierng
ID: 22740041
string is a ref type though whereas char is value, so I would have assumed 2 is optimal.

divyeshhdoshi, can you explain what you mean?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22740413
as far as I know, there is not StringBuilder.ToCharArray() array method
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 22740571
Using .Net Reflector, here is the source for GetBytes(string)
public virtual byte[] GetBytes(string s)
{
    if (s == null)
    {
        throw new ArgumentNullException("s", Environment.GetResourceString("ArgumentNull_String"));
    }
    char[] chars = s.ToCharArray();
    return this.GetBytes(chars, 0, chars.Length);
}

 
so it looks like 2. byte[] the_array = System.Text.Encoding.UTF8.GetBytes(the_stringbuilder.ToCharArray()); is a LITTLE faster because that is where GetBytes(String) ends up calling anyway.  But it matters little unless you are calling GetBytes() inside an array that gets executed 1000 times.  We are probably talking the difference of a few milliseconds here!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22740615
@MogalManic,
>>so it looks like 2. byte[] the_array = System.Text.Encoding.UTF8.GetBytes(the_stringbuilder.ToCharArray()); is a LITTLE faster

So, you have compiled that code? Did it compile? Are you talking with fundaments or just speculating?
0
 
LVL 6

Expert Comment

by:mirzas
ID: 22740880
Just use 2.

feel free to ignore my suggestion.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22740971
same question for mirzas.
0
 

Author Comment

by:gustierng
ID: 22755754
Sorry, perhaps this should have said: CopyTo(...) instead of ToCharArray().

If so which would be optimal?
0
 
LVL 6

Accepted Solution

by:
mirzas earned 500 total points
ID: 22755979
ToString() methods should be avoided as they are designed to show a string representation of a class
and are likely to be overridden.



+ Strictly comparing the performance of String vs array of Chars I would go for Chars any time since
String class adds more overhead and contains methods and properties that you generally don't need.

+ StringBuilder does not contain a ToCharArray() method, but String does. I assume you attempted to use this method on a String object.  

If you are really using a string builder and converting to byte[] array, this code is fine:

byte[] the_array = System.Text.Encoding.UTF8.GetBytes(the_stringbuilder.ToString());

Converting from String to CharArray will cost you 4 additional branching operations and one additional memory copy.

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 47
C# parent child form 5 28
Switching Visual Studio 2015 to normal debug display 3 25
Problem with SelectList in .NET MVC application 1 11
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question