Solved

converting StringBuilder to byte[]

Posted on 2008-10-17
10
6,335 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now