Skip navigation

Category Archives: Cs

Just cast it (int)enumVal:


enum BodyType
{
  Fat,
  Thin,
  Musclebound = 500,
  Anorexic
} ;

class Program
{
  static void Main( string[] args )
  {
    BodyType debbie = BodyType.Fat ;
    BodyType john = BodyType.Anorexic ;
    BodyType harold = BodyType.Thin ;

    Console.WriteLine( "Debbie is " + debbie + ".  Numerical value: " + (int)debbie ) ;
    Console.WriteLine( "john is " + john + ".  Numerical value: " + (int)john ) ;
    Console.WriteLine( "harold is " + harold + ".  Numerical value: " + (int)harold ) ;
  }
}
 

Advertisements

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 );
    }
  }
}