Revision: 16779
Updated Code
at August 15, 2009 18:29 by jimfred
Updated Code
using System.Diagnostics.Debug; // for Assert using System.Net.IPAddress; // for NetworkToHostOrder Assert(0x12345678 == NetworkToHostOrder( 0x78563412)); // 4-byte Assert(0x12340000 == NetworkToHostOrder( 0x3412)); // 4-byte Assert(0x12000000 == NetworkToHostOrder( 0x12)); // 4-byte Assert( 0x1234 == NetworkToHostOrder((short)0x3412)); // 2-byte Assert( 0x1200 == NetworkToHostOrder( (short)0x12)); // 2-byte Assert(0x123456789abcde10 == NetworkToHostOrder(0x10debc9a78563412)); // 8-byte // using 'unchecked()' to convert large UInt32 values to negative Int32 values. // See stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c // In this usage it'ss like the reinterpret cast because bits remain unchanged. // Without unchecked, a runtime exception will be thrown // if the result becomes negative. Assert( 0xFF1234FF == (UInt32)NetworkToHostOrder(unchecked((Int32)0xFF3412FF)));
Revision: 16778
Updated Code
at August 15, 2009 18:26 by jimfred
Updated Code
using System.Diagnostics.Debug; // for Assert using System.Net.IPAddress; // for NetworkToHostOrder Assert(0x12345678 == NetworkToHostOrder( 0x78563412)); // 4-byte Assert(0x12340000 == NetworkToHostOrder( 0x3412)); // 4-byte Assert(0x12000000 == NetworkToHostOrder( 0x12)); // 4-byte Assert( 0x1234 == NetworkToHostOrder((short)0x3412)); // 2-byte Assert( 0x1200 == NetworkToHostOrder( (short)0x12)); // 2-byte Assert(0x123456789abcde10 == NetworkToHostOrder(0x10debc9a78563412)); // 8-byte // using unchecked to convert large UInt32 values to negative Int32 values. // See stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c // unchecked in this usage is like the reinterpret cast because // it leaves the bits unchanged. // Without unchecked, a runtime exception will be thrown // if the result becomes negative. Assert( 0xFF1234FF == (UInt32)NetworkToHostOrder(unchecked((Int32)0xFF3412FF)));
Revision: 16777
Updated Code
at August 15, 2009 18:22 by jimfred
Updated Code
using System.Diagnostics.Debug; // for Assert using System.Net.IPAddress; // for NetworkToHostOrder Assert(0x12345678 == NetworkToHostOrder(0x78563412)); // 4-byte Assert(0x12340000 == NetworkToHostOrder(0x3412)); // 4-byte Assert(0x12000000 == NetworkToHostOrder(0x12)); // 4-byte Assert(0x1234 == NetworkToHostOrder((short)0x3412)); // 2-byte Assert(0x1200 == NetworkToHostOrder((short)0x12)); // 2-byte Assert(0x123456789abcde10 == NetworkToHostOrder(0x10debc9a78563412)); // 8-byte // using unchecked to convert large UInt32 values to negative Int32 values. // See stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c // unchecked in this usage is like the reinterpret cast because // it leaves the bits unchanged. Assert( 0xFF1234FF == (UInt32)NetworkToHostOrder(unchecked((Int32)0xFF3412FF)));
Revision: 16776
Updated Code
at August 15, 2009 18:18 by jimfred
Updated Code
System.Diagnostics.Debug.Assert(0x12345678 == System.Net.IPAddress.NetworkToHostOrder(0x78563412)); // 4-byte System.Diagnostics.Debug.Assert(0x12340000 == System.Net.IPAddress.NetworkToHostOrder(0x3412)); // 4-byte System.Diagnostics.Debug.Assert(0x12000000 == System.Net.IPAddress.NetworkToHostOrder(0x12)); // 4-byte System.Diagnostics.Debug.Assert(0x1234 == System.Net.IPAddress.NetworkToHostOrder((short)0x3412)); // 2-byte System.Diagnostics.Debug.Assert(0x1200 == System.Net.IPAddress.NetworkToHostOrder((short)0x12)); // 2-byte System.Diagnostics.Debug.Assert(0x123456789abcde10 == System.Net.IPAddress.NetworkToHostOrder(0x10debc9a78563412)); // 8-byte int // Example using unchecked to convert large UInt32 values to negative Int32 values. // See http://stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c // unchecked in this usage is like the reinterpret cast because it leaves the bits unchanged. System.Diagnostics.Debug.Assert( 0xFF1234FF == (UInt32)System.Net.IPAddress.NetworkToHostOrder(unchecked((Int32)0xFF3412FF)));
Revision: 16775
Updated Code
at August 15, 2009 18:17 by jimfred
Updated Code
System.Diagnostics.Debug.Assert(0x12345678 == System.Net.IPAddress.NetworkToHostOrder(0x78563412)); // 4-byte System.Diagnostics.Debug.Assert(0x12340000 == System.Net.IPAddress.NetworkToHostOrder(0x3412)); // 4-byte System.Diagnostics.Debug.Assert(0x12000000 == System.Net.IPAddress.NetworkToHostOrder(0x12)); // 4-byte System.Diagnostics.Debug.Assert(0x1234 == System.Net.IPAddress.NetworkToHostOrder((short)0x3412)); // 2-byte System.Diagnostics.Debug.Assert(0x1200 == System.Net.IPAddress.NetworkToHostOrder((short)0x12)); // 2-byte System.Diagnostics.Debug.Assert(0x123456789abcde10 == System.Net.IPAddress.NetworkToHostOrder(0x10debc9a78563412)); // 8-byte int // Example using unchecked to convert large UInt32 values to negative Int32 values. // See http://stackoverflow.com/questions/1131843/how-do-i-convert-uint-to-int-in-c // unchecked in this usage is like the reinterpret cast because it leaves the bits unchanged. System.Diagnostics.Debug.Assert( 0xFF1234FF == (UInt32)System.Net.IPAddress.NetworkToHostOrder(unchecked((Int32)0xFF3412FF)));
Revision: 16774
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 15, 2009 14:33 by jimfred
Initial Code
System.Diagnostics.Debug.Assert(0x12345678 == System.Net.IPAddress.NetworkToHostOrder(0x78563412)); // 4-byte System.Diagnostics.Debug.Assert(0x12340000 == System.Net.IPAddress.NetworkToHostOrder(0x3412)); // 4-byte System.Diagnostics.Debug.Assert(0x12000000 == System.Net.IPAddress.NetworkToHostOrder(0x12)); // 4-byte System.Diagnostics.Debug.Assert(0x1234 == System.Net.IPAddress.NetworkToHostOrder((short)0x3412)); // 2-byte System.Diagnostics.Debug.Assert(0x1200 == System.Net.IPAddress.NetworkToHostOrder((short)0x12)); // 2-byte System.Diagnostics.Debug.Assert(0x123456789abcde10 == System.Net.IPAddress.NetworkToHostOrder(0x10debc9a78563412)); // 8-byte int
Initial URL
Initial Description
This shows how to implement simple byte swapping in C#. It's shown in the context of assert statements instead of print statements to show the expected results. Interesting post http://bytes.com/topic/c-sharp/answers/506861-littleendian-bigendian compared performance of various methods. mask-shift-or performed best.
Initial Title
byte swapping using System.Net.IPAddress.NetworkToHostOrder
Initial Tags
Initial Language
C#