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 7368159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:37:48+00:00 2026-05-29T03:37:48+00:00

I need to write a C# string to a preallocated unmanaged buffer encoded as

  • 0

I need to write a C# string to a preallocated unmanaged buffer encoded as Utf8. Before answering, please read the following requirements:

  • No new allocations (so please, don’t direct me to answers involving creating byte arrays or other instantiations)
  • No transitions to unmanaged code (no pinvoke/calli)

Currently, I’m using OpCodes.Cpblk to copy raw strings from C# to unmanaged buffers using 16 bit characters. This gives me roughly the same performance as using unmanaged memcpy on an x64 architecture and I really need the throughput to be close to that.

I am considering fixing the string as a char* and iterating over it, but implementing an encoder without jump tables would be both cumbersome and less than optimal when it comes to performance.

  • 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. Editorial Team
    Editorial Team
    2026-05-29T03:37:48+00:00Added an answer on May 29, 2026 at 3:37 am

    Use the unsafe overload

    public override unsafe int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
    

    of the UTF8Encoding-class. You need to specify pointers to the string and the byte-buffer that will receive the chars. It will copy UTF-8 chars into it. No allocations will be happening but it will require unsafe code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write string containing base64 encoded text to a text file and
I need to write the following string to a txt a File: SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ FELIX
I need to write a little program in C that parses a string. I
I need to write string to an xml file in Silverlight. When I used
I need to write a string literal to a text file, but the C#
I have a page www.senderdomain.com/sender.aspx , from which i need to write a string
I need explanations.. I using C#.NET to web applications, I always write: string val
I need to write stored procedure that gets string. Each char in string have
I need to write method that will chek String str on other string, and
i need to write sql query in string and pass it to linq like

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.