Solved

converting StringBuilder to byte[]

Posted on 2008-10-17
10
7,117 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
Industry Leaders: 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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