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. C# Converting socket communitation ( byte[] ) to string

C# Converting socket communitation ( byte[] ) to string

Scheduled Pinned Locked Moved C#
questioncsharpxmlhelplearning
5 Posts 5 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
    nyjcr
    wrote on last edited by
    #1

    Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets

                        Byte\[\] received = new Byte\[256\];
                        int bytesReceived = server1Tcp.Receive(input, input.Length, 0);
                        dataReceived = System.Text.Encoding.ASCII.GetString(input);
    

    How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC

    M H A G 4 Replies Last reply
    0
    • N nyjcr

      Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets

                          Byte\[\] received = new Byte\[256\];
                          int bytesReceived = server1Tcp.Receive(input, input.Length, 0);
                          dataReceived = System.Text.Encoding.ASCII.GetString(input);
      

      How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC

      M Offline
      M Offline
      Michael9000
      wrote on last edited by
      #2

      Add this at below the posted code

      dataReceived.Trim();

      1 Reply Last reply
      0
      • N nyjcr

        Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets

                            Byte\[\] received = new Byte\[256\];
                            int bytesReceived = server1Tcp.Receive(input, input.Length, 0);
                            dataReceived = System.Text.Encoding.ASCII.GetString(input);
        

        How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC

        H Offline
        H Offline
        hammerstein05
        wrote on last edited by
        #3

        Trim the input, IIRC you get the full buffer length in string.

        1 Reply Last reply
        0
        • N nyjcr

          Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets

                              Byte\[\] received = new Byte\[256\];
                              int bytesReceived = server1Tcp.Receive(input, input.Length, 0);
                              dataReceived = System.Text.Encoding.ASCII.GetString(input);
          

          How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          The way I would do it is to send the length of the string, and then the string itself. So you would have something like:

          byte[] myBytes = ...;
          //Read the first four bytes and get an int
          int length = BitConverter.ToInt32(myBytes, 0);
          //Start at the 4th byte (after the int) and read until the end of the string
          string myString = System.Text.Encoding.ASCII.GetString(myBytes, 4, length);

          My current favourite word is: I'm starting to run out of fav. words!

          -SK Genius

          Game Programming articles start -here[^]-

          1 Reply Last reply
          0
          • N nyjcr

            Hi, Sorry if this is a simple question, I'm learning c# and I'm trying to send a string to another computer, but when I receive the string I want to store that string in an xml file the problem is that the string comes with "empty" characters at the end. so my xml looks like "abc.........) follow by many empty chars. here is the code I'm using to get the data. using tcp sockets

                                Byte\[\] received = new Byte\[256\];
                                int bytesReceived = server1Tcp.Receive(input, input.Length, 0);
                                dataReceived = System.Text.Encoding.ASCII.GetString(input);
            

            How can I save the "dataReceived" string without the empty chars. again, thank you to all of you who have helped me learn C# -JC

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            nyjcr wrote:

            the problem is that the string comes with "empty" characters at the end.

            No, the string doesn't come with any empty characters. The problem is that you get the length of the actual data, but then you just ignore that and convert the entire buffer into a string. The unused part of the buffer just happens to be filled with zeroes. Only convert the actual data: dataReceived = System.Text.Encoding.ASCII.GetString(input, 0, bytesReceived);

            Despite everything, the person most likely to be fooling you next is yourself.

            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