locked
byte array RRS feed

  • Question

  • Hi all

     

    I have a text to send over TCP socket:

     

    Code Snippet

    public Socket mySendSocket;

    ...................

    byte[] byteData = System.Text.Encoding.ASCII.GetBytes("hello World");

     

    mySendSocket.Send(byteData);

     

    ................................................

     

    But my client programm require zero on the end of the socket string.

     

    Haw can I do it:

     

    Haw add 0 to the end off byte array?

    Is it possible to set Socket to send 0x00 on the end?

     

    Thanks in advance

    Monday, July 2, 2007 5:33 PM

Answers

  • Code Snippet

    Dim test() As Byte = System.Text.Encoding.ASCII.GetBytes("Hello World")

    Dim test2(test.Length) As Byte

     

    For x As Integer = 1 To test.Length - 1

       test2(x) = test(x)

    Next

     

    test2(test.Length) = 0

     

    MsgBox(test2(test.Length))

     

     

    Adamus

    Monday, July 2, 2007 6:16 PM
  •  

    Code Snippet

    byte[] byteData = System.Text.Encoding.ASCII.GetBytes("hello World\0");

     

    (In .Net, a string has a length.  It's not terminated at a NUL character)

    Monday, July 2, 2007 6:18 PM
  • James Curran's version is more efficient. It doesn't involve making a copy of the data.

     

    Or you could just call Send a second time to send the extra byte...that's probably the simplest solution!

    Monday, July 2, 2007 9:07 PM

All replies

  • Code Snippet

    Dim test() As Byte = System.Text.Encoding.ASCII.GetBytes("Hello World")

    Dim test2(test.Length) As Byte

     

    For x As Integer = 1 To test.Length - 1

       test2(x) = test(x)

    Next

     

    test2(test.Length) = 0

     

    MsgBox(test2(test.Length))

     

     

    Adamus

    Monday, July 2, 2007 6:16 PM
  •  

    Code Snippet

    byte[] byteData = System.Text.Encoding.ASCII.GetBytes("hello World\0");

     

    (In .Net, a string has a length.  It's not terminated at a NUL character)

    Monday, July 2, 2007 6:18 PM
  • Thanks for quick replay

     

    Jeff give me an answer what I use:

     

    int length = byteData.Length;
    Array.Resize(ref byteData, length + 1); //the "ref" might not be necessary
    byteData[length] = 0;

    Monday, July 2, 2007 8:12 PM
  • James Curran's version is more efficient. It doesn't involve making a copy of the data.

     

    Or you could just call Send a second time to send the extra byte...that's probably the simplest solution!

    Monday, July 2, 2007 9:07 PM