Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8274
  • Last Modified:

converting StringBuilder to byte[]

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
gustierng
Asked:
gustierng
  • 3
  • 3
  • 2
  • +2
1 Solution
 
mirzasCommented:
2.
0
 
divyeshhdoshiCommented:
both are optimal
because both are using the same procedure to follow.


0
 
gustierngAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Jaime OlivaresSoftware ArchitectCommented:
as far as I know, there is not StringBuilder.ToCharArray() array method
0
 
MogalManicCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
@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
 
mirzasCommented:
Just use 2.

feel free to ignore my suggestion.
0
 
Jaime OlivaresSoftware ArchitectCommented:
same question for mirzas.
0
 
gustierngAuthor Commented:
Sorry, perhaps this should have said: CopyTo(...) instead of ToCharArray().

If so which would be optimal?
0
 
mirzasCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now