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

  • Home
  • SEARCH
  • 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 5978321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:29:57+00:00 2026-05-22T21:29:57+00:00

I have a piece of code that converts string into memory stream: using (MemoryStream

  • 0

I have a piece of code that converts string into memory stream:

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(applicationForm)))

However I’m a bit confused if it’s correct. Basically I’m always confused about .NET encoding.

Bottom line: do I use correct encoding object (UTF8) to get bytes?

I know that internally .NET stores string as UTF-16, but my applicationForm variable was based on file with text which was saved in UTF-8 encoding.

Thanks,Pawel

EDIT 1: Let’s explain exactly how I get applicationForm variable. I do have access to assembly that exposes class with method GenerateApplicationForm. That method returns string. However I know, that somewhere behind the scenes, component uses files stored on drive.Content of those files are encoded using UTF-8. So I can’t read file directly etc. I only have that string and I know, that originally, UTF-8 encoded file is used.
In client code, the one that used GenerateApplicationForm component, I have to convert applicationForm variable into stream, cos other components (from another assembly) is expecting a Stream. That’s where using…. statement mentioned in question springs into action.

  • 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-22T21:29:57+00:00Added an answer on May 22, 2026 at 9:29 pm

    Assuming applicationForm is a string you read from some UTF8 text file. It will be UTF16/Unicode, whatever the encoding of the source file. The conversion happened when you loaded the file into the string.

    Your code will encode the applicationForm string into a MemoryStream of UTF8 bytes.

    This may or may not be correct depending on what you want to do with it.

    .Net strings are always UTF16 or Unicode. When Strings are converted to files, streams or byte[], they can be encoded in different ways. 1 byte is not enough to store all the different characters used in all languages so more complicated strings need to be encoded so one character can be represented by more than one byte, Sometimes or always depending on the encoding used.

    If you use a simple encoding like ASCII one character will always comprise of one byte but the data will be limited to the ASCII character set. Converting to ‘ASCII’ from any UTF encoding could lose data if any multi-byte characters are used.

    For the complete picture on unicode go here.

    EDIT 1:
    Barring further info on the GenerateApplicationForm component, enconding UTF8 is likely to be the right choice. If that doesn’t work, try ASCII or UTF16. Best of all, consult the component source code or the component provider.

    EDIT 2:
    Definitely UTF8 then, you were right all along.

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

Sidebar

Related Questions

I have a piece of C# code that needs to convert a string array
I have a piece of code that converts an infix expression to an expression
I have a piece of code that load a very big image in memory.
I have a piece of code that creates dynamic controls using 2 while loops.
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that takes several rows from a database and
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that calls a WCF service that is hosted
If have a piece of code that gets some data from a sql database

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.