In network communication, numerical data transmission is common. However, numerical data is stored in uneven tables and hardware in different storage modes. There are two main categories: high storage and low storage. net platform is low-level storage, through. net provides low-level function reading and writing and does not provide settings; for High-level storage, Java is a popular language platform. Because the storage is different, the conversion is required when reading and writing data. In. net, you can simply reverse the array or shift the storage to perform high conversion. However, the program has been written, and the reading and writing time is low. If you do not want to modify the read/write code, you can simply convert the values using the following functions.
Code (found on a foreigner's website)
Class Endian
{
Public static short SwapInt16 (short v)
{
Return (short) (v & 0xff) <8) | (v> 8) & 0xff ));
}
Public static ushort SwapUInt16 (ushort v)
{
Return (ushort) (v & 0xff) <8) | (v> 8) & 0xff ));
}
Public static int SwapInt32 (int v)
{
Return (int) (SwapInt16 (short) v) & 0 xffff) <0x10) |
(SwapInt16 (short) (v> 0x10) & 0 xffff ));
}
Public static uint SwapUInt32 (uint v)
{
Return (uint) (SwapUInt16 (ushort) v) & 0 xffff) <0x10) |
(SwapUInt16 (ushort) (v> 0x10) & 0 xffff ));
}
Public static long SwapInt64 (long v)
{
Return (long) (SwapInt32 (int) v) & 0 xffffffffL) <0x20) |
(SwapInt32 (int) (v> 0x20) & 0 xffffffffL ));
}
Public static ulong SwapUInt64 (ulong v)
{
Return (ulong) (SwapUInt32 (uint) v) & 0 xffffffffL) <0x20) |
(SwapUInt32 (uint) (v> 0x20) & 0 xffffffffL ));
}
}
The above method is used to convert the data between high and low bits, that is, high to low and low to high are feasible.
Use
On the. net platform, BitConverter provides low-level numeric storage.
? 1
2 int a = 1;
Byte [] data = BitConverter. GetBytes ();
The byte obtained from the above Code is [,]
? 1
2 a = Endian. SwapInt32 ();
Data = BitConverter. GetBytes ();
Before GetBytes, convert the preceding function to obtain the result of high-level storage [0, 0, 1].
Summary
This is not taken into account at the lower layer when writing Beetle, so the scope of modification to byte [] is relatively large, this function simplifies a lot of work and reduces the testing time.
/// <Summary>
/// Write a ushort
/// </Summary>
/// <Param name = "value"> ushort </param>
Public unsafe void Write (ushort value)
{
If (! LittleEndian)
Value = Endian. SwapUInt16 (value );
If (mCurrentBuffer. CanWrite (2 ))
{
Fixed (byte * ptr = & mCurrentBuffer. Data [mCurrentBuffer. Postion])
{
* (Ushort *) ptr = value;
}
MCurrentBuffer. Add (2 );
MLength + = 2;
}
Else
{
Fixed (byte * ptr = & mTempData [0])
{
* (Ushort *) ptr = value;
}
Write (mTempData, 0, 2 );
}
}
/// <Summary>
/// Read a short Value
/// </Summary>
/// <Returns> short </returns>
Public unsafe short ReadShort ()
{
Short result;
If (mCurrentBuffer. CanRead (2) = 2)
{
Fixed (byte * ptr = & mCurrentBuffer. Data [mCurrentBuffer. Postion])
{
Result = * (short *) ptr;
}
MCurrentBuffer. Read (2 );
}
Else
{
Read (mTempData, 0, 2 );
Result = BitConverter. ToInt16 (mTempData, 0 );
}
If (! LittleEndian)
Result = Endian. SwapInt16 (result );
Return result;
}
If your lower-level code is written, you only need to add Endian processing at the entrance and exit of your code.