Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Question about UTF8 Encoding

Question about UTF8 Encoding

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NorthWoodsman
    wrote on last edited by
    #1

    How do I take an arbitrary string and UTF8 encode it using System.Text.Encoding.UTF8? I found out how to write strings and how to convert byte data, but not how to convert strings. You'd think this would be easier; maybe I'm missing something. (I'm using C# by the way).

    L 1 Reply Last reply
    0
    • N NorthWoodsman

      How do I take an arbitrary string and UTF8 encode it using System.Text.Encoding.UTF8? I found out how to write strings and how to convert byte data, but not how to convert strings. You'd think this would be easier; maybe I'm missing something. (I'm using C# by the way).

      L Offline
      L Offline
      Laimis
      wrote on last edited by
      #2

      Use Encoding class in System.Text namespace. Basically to convert, you first set up encoding. Let's say you have a simple string that you named simpleS. Here is the code that would convert: Encoding ascii = Encoding.ASCII; Encoding utf8 = Encoding.UTF8; byte[] simpleBytes = ascii.GetBytes(simpleS); byte[] newbytes = Encoding.Encode(ascii, utf8, simpleBytes); that's it. Key here is static Encode method provided by Encoding class. Once you have the new byte array, you can convert it to char array of utf8 encoded chars. I hope this is of some help. MSDN documentation also has a very similar example and more stuff on this encoding. I know I used it for the project I worked some time ago.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups