2

I try to transfer a filedata with sockets via a sslStream. It seems that I must send the file data length before the data. The problem is that the code

Byte [] size = BitConverter.GetBytes(fileData.Length)

Returns in little endian but internet protocols are using big endian.

How I can convert this to big endian and write it to stream;

Peter O.
  • 32,158
  • 14
  • 82
  • 96
user1654278
  • 67
  • 1
  • 10
  • 1
    Possible duplicate of [How to get little endian data from big endian in c# using bitConverter.ToInt32 method?](http://stackoverflow.com/questions/8241060/how-to-get-little-endian-data-from-big-endian-in-c-sharp-using-bitconverter-toin) – Umut Seven Dec 10 '15 at 12:42

1 Answers1

2

I assume that you want to send just one int value. The order of the bytes should be reversed before transmitting:

byte[] bytes = BitConverter.GetBytes(fileData.Length);
if (BitConverter.IsLittleEndian)
     Array.Reverse(bytes); 

Check this MSDN page to get more information about BitConverted.

You could also convert it using HostToNetworkOrder, for example:

int reversed = IPAddress.HostToNetworkOrder(fileData.Length);
byte[] bytes = BitConverter.GetBytes(reversed);

To send these bytes use the Write method:

stream.Write(bytes, 0, bytes.Length);
Sergii Zhevzhyk
  • 4,074
  • 22
  • 28