?
Solved

converting StringBuilder to byte[]

Posted on 2008-10-17
10
Medium Priority
?
7,398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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