none
TcpClient - Big Endian, Little Endian RRS feed

  • Question

  • Hello,
    I implemented it that way, it works. If it has to be different, why always, are there better ways?
    Could I use a ready-made function? From a library, if so, what's its name?
    // Insert a 4-byte length of the message as Big Endian 
    message = message.Insert(0, "    ");
    byte[] messAsByte = System.Text.Encoding.ASCII.GetBytes(message);
    int length = message.Length;
    byte[] byteView = BitConverter.GetBytes(length);
    messAsByte[0] = byteView[3];
    messAsByte[1] = byteView[2];
    messAsByte[2] = byteView[1];
    messAsByte[3] = byteView[0];
    bool retVal = socketClient.send(messAsByte);


    Thanks in advance.
    Best regards, Markus
    Friday, February 15, 2019 5:28 PM

Answers

  • BitConverter.GetBytes, just like you're doing now.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Markus Freitag Sunday, February 17, 2019 1:53 PM
    Sunday, February 17, 2019 6:05 AM
  • Check this too:

    byte[] message_bytes = Encoding.ASCII.GetBytes( message );
    byte[] length_bytes = BitConverter.GetBytes( IPAddress.HostToNetworkOrder( message_bytes.Length ) );
     
    socketClient.Send( length_bytes );
    socketClient.Send( message_bytes );


    • Edited by Viorel_MVP Sunday, February 17, 2019 7:39 AM
    • Marked as answer by Markus Freitag Sunday, February 17, 2019 1:53 PM
    Sunday, February 17, 2019 7:38 AM

All replies

  • Check this too:

    using( var ns = new NetworkStream( socketClient, false ) )
    {
        using( var sw = new StreamWriter( ns, Encoding.UTF8 ) )
        {
            sw.Write( message );
        }
    }
    

    Friday, February 15, 2019 6:07 PM
  • I have seen this suggestion:

    BitConverter.ToUInt32(BitConverter.GetBytes(g).Reverse().ToArray<byte>(), 0);

    but it may be the fastest method is to do it the old fashioned way:

            static uint ByteSwapDword(uint word)
            {
                return ((word >> 24) & 0x000000FF) | ((word >> 8) & 0x0000FF00) | ((word << 8) & 0x00FF0000) | ((word << 24) & 0xFF000000);            
            }
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, February 15, 2019 11:11 PM
  • > new StreamWriter( ns, Encoding.UTF8 ) ) You mean the Encoding.UTF8 do that automatically? Best regards, Markus
    Saturday, February 16, 2019 11:19 AM
  • message = message.Insert(0, "    ");

    Hello Tim, 

    >static uint ByteSwapDword(uintword)

    and how I insert it in my message?

    Best regards, Markus

    Saturday, February 16, 2019 11:20 AM
  • BitConverter.GetBytes, just like you're doing now.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Markus Freitag Sunday, February 17, 2019 1:53 PM
    Sunday, February 17, 2019 6:05 AM
  • Check this too:

    byte[] message_bytes = Encoding.ASCII.GetBytes( message );
    byte[] length_bytes = BitConverter.GetBytes( IPAddress.HostToNetworkOrder( message_bytes.Length ) );
     
    socketClient.Send( length_bytes );
    socketClient.Send( message_bytes );


    • Edited by Viorel_MVP Sunday, February 17, 2019 7:39 AM
    • Marked as answer by Markus Freitag Sunday, February 17, 2019 1:53 PM
    Sunday, February 17, 2019 7:38 AM