Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:54:30+00:00 2026-05-10T14:54:30+00:00

While there are 100 ways to solve the conversion problem, I am focusing on

  • 0

While there are 100 ways to solve the conversion problem, I am focusing on performance.

Give that the string only contains binary data, what is the fastest method, in terms of performance, of converting that data to a byte[] (not char[]) under C#?

Clarification: This is not ASCII data, rather binary data that happens to be in a string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:54:31+00:00Added an answer on May 10, 2026 at 2:54 pm

    I’m not sure ASCIIEncoding.GetBytes is going to do it, because it only supports the range 0x0000 to 0x007F.

    You tell the string contains only bytes. But a .NET string is an array of chars, and 1 char is 2 bytes (because a .NET stores strings as UTF16). So you can either have two situations for storing the bytes 0x42 and 0x98:

    1. The string was an ANSI string and contained bytes and is converted to an unicode string, thus the bytes will be 0x00 0x42 0x00 0x98. (The string is stored as 0x0042 and 0x0098)
    2. The string was just a byte array which you typecasted or just recieved to an string and thus became the following bytes 0x42 0x98. (The string is stored as 0x9842)

    In the first situation on the result would be 0x42 and 0x3F (ascii for ‘B?’). The second situation would result in 0x3F (ascii for ‘?’). This is logical, because the chars are outside of the valid ascii range and the encoder does not know what to do with those values.

    So i’m wondering why it’s a string with bytes?

    • Maybe it contains a byte encoded as a string (for instance Base64)?
    • Maybe you should start with an char array or a byte array?

    If you realy do have situation 2 and you want to get the bytes out of it you should use the UnicodeEncoding.GetBytes call. Because that will return 0x42 and 0x98.

    If you’d like to go from a char array to byte array, the fastest way would be Marshaling.. But that’s not really nice, and uses double memory.

    public Byte[] ConvertToBytes(Char[] source) {     Byte[] result = new Byte[source.Length * sizeof(Char)];     IntPtr tempBuffer = Marshal.AllocHGlobal(result.Length);     try     {         Marshal.Copy(source, 0, tempBuffer, source.Length);         Marshal.Copy(tempBuffer, result, 0, result.Length);     }     finally     {         Marshal.FreeHGlobal(tempBuffer);     }     return result; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While there are multiple ways to reverse bit order in a byte, I'm curious
While there are many good online and offline tools for testing regular expressions, I
While there are a handful of great programs for ISV Startups (BizSpark, Emplower ISV,
While there are many guides for Browser Helper Objects, i have a hard time
I can't wget while there is no path already to save. I mean, wget
I'm new at javascript and while there are many more complex solutions, I don't
I'm new to coffeescript, and while there is extensive information concerning the proper method
This is related to another open question of mine . While there aren't any
I work in the technical group at a large Architecture firm. While there are
We need to create a basic PDF reader running on J2ME. While there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.