. Net is a simple and quick way to switch between values between high and low bits.

Source: Internet
Author: User

 
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.