htons in c#

htonl in c#

HostToNetworkOrder function resides in the System.Net.IPAddress object (what a weird place to put it!)

3 overloads are provided:

System.Net.IPAddress.HostToNetworkOrder( short val )
System.Net.IPAddress.HostToNetworkOrder( int val )
System.Net.IPAddress.HostToNetworkOrder( long val )

Sending binary data in C#

Use the BitConverter class

using System ;
using System.Net ;

class Program
{
  static void Main( string[] args )
  {
    int val = 0x0112A380 ;
    val = IPAddress.HostToNetworkOrder( val );

    Console.WriteLine("Val is {0}", val ) ;
    Console.WriteLine( "Little endian? " + BitConverter.IsLittleEndian ) ;

    byte[] bytes = BitConverter.GetBytes( val );
    foreach( byte b in bytes )
    {
      Console.WriteLine( b );
    }
  }
}

Post a Comment