Write utf8 encoded string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samuel

    Write utf8 encoded string


    Hi

    I am trying to write to a string text encoded to utf8 as oppose to utf16

    Since the data comes from an XML object (and I serialize it) I need to pass
    either StreamWriter or a StringWriter object, I don't want to create a file
    so I want to use a StringWriter (passing to it's constructor a
    StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
    know how to change it)

    Any Advice?

    Thank you in advance,
    Samuel


  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Write utf8 encoded string

    Samuel wrote:
    I am trying to write to a string text encoded to utf8 as oppose to utf16
    >
    Since the data comes from an XML object (and I serialize it) I need to pass
    either StreamWriter or a StringWriter object, I don't want to create a file
    so I want to use a StringWriter (passing to it's constructor a
    StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
    know how to change it)
    No, a StringWriter doesn't encode anything at all. What you write to it
    ends up in the internal StringBuilder as characters, not encoded as bytes.

    Why do you think that the StringWriter would encode the text? How are
    you using it?

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Write utf8 encoded string

      "Samuel" <samuel.shulman @ntlworld.comsc hrieb:
      I am trying to write to a string text encoded to utf8 as oppose to utf16
      >
      Since the data comes from an XML object (and I serialize it) I need to
      pass either StreamWriter or a StringWriter object, I don't want to create
      a file so I want to use a StringWriter (passing to it's constructor a
      StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
      know how to change it)
      Strings in .NET are always encoded in UTF-16 when stored in memory. You
      cannot change that. If you want to get the byte representation of a string
      in a certain encoding, check 'System.Text.En coding.GetBytes '.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Patrice

        #4
        Re: Write utf8 encoded string

        The String type deals with utf16 strings only. Of course you can use an utf8
        encoding when reading/writing from/to an external destination (such as a
        file or a web browser response stream).

        Not sure why you want to keep the string using utf8 but I'm afraid that
        depending on what you'll do with the string it could well introduce some
        subtil problems (I would keep it a s abyte array in the worst case if
        possible)...

        You may want to elaborate about what you are trying to do and in particular
        what you'll do with this string once encoded using utf8 (for example even
        with creating mails the idea is that .NET always uses utf16 but you can ask
        to encode as utf8 when you write youtr content to the mail object, a file, a
        browser stream, a database etc...)
        --
        Patrice

        "Samuel" <samuel.shulman @ntlworld.coma écrit dans le message de groupe de
        discussion : Oa#Brcn8IHA.233 6@TK2MSFTNGP03. phx.gbl...
        >
        Hi
        >
        I am trying to write to a string text encoded to utf8 as oppose to utf16
        >
        Since the data comes from an XML object (and I serialize it) I need to
        pass either StreamWriter or a StringWriter object, I don't want to create
        a file so I want to use a StringWriter (passing to it's constructor a
        StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
        know how to change it)
        >
        Any Advice?
        >
        Thank you in advance,
        Samuel
        >
        >

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Write utf8 encoded string

          >>THIS MAY NOT BE THE ACTUAL ENCODING

          Herfried wrote:
          STRINGS IN .NET ARE ALWAYS ENCODED IN UTF-16 WHEN STORED IN MEMORY


          Comment

          Working...