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. C#
  4. char[] to string conversion

char[] to string conversion

Scheduled Pinned Locked Moved C#
cssquestion
3 Posts 3 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    I've got char[] buffer = new char[N]; with some unicode text. The message is less than N and all other elements are 0 in the buffer. But when I create string from it with string str = new string(buffer); it appends all N elements including terminating zero characters that string looks like: "message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars. Why it does not terminate it?

    Чесноков

    K OriginalGriffO 2 Replies Last reply
    0
    • C Chesnokov Yuriy

      I've got char[] buffer = new char[N]; with some unicode text. The message is less than N and all other elements are 0 in the buffer. But when I create string from it with string str = new string(buffer); it appends all N elements including terminating zero characters that string looks like: "message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars. Why it does not terminate it?

      Чесноков

      K Offline
      K Offline
      Kubajzz
      wrote on last edited by
      #2

      This should do the job:

      string str = new string(buffer).TrimEnd('\0');

      Or, if you know the desired length of the string, you can try this:

      string str = new string(buffer, 0, length);

      1 Reply Last reply
      0
      • C Chesnokov Yuriy

        I've got char[] buffer = new char[N]; with some unicode text. The message is less than N and all other elements are 0 in the buffer. But when I create string from it with string str = new string(buffer); it appends all N elements including terminating zero characters that string looks like: "message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars. Why it does not terminate it?

        Чесноков

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Because C# strings are not null terminated, unlike C / C++. Because the char[] has a length, each character is transferred into a new string of the same length. Use Trim or the buffer convert as Kubajzz suggested.

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        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