The conversion between int, String, byte, decimal and hexadecimal is often used in network communication

Turn int byte

int i = 12;
byte b = (byte)i;
Copy the code

Byte turn int

i = (int)b;
Copy the code

Turn int byte []

int startNum = 18888; byte[] by = BitConverter.GetBytes(startNum); / / 200 73 0 0Copy the code

Turn int byte []

int num = BitConverter.ToInt32(by, 0); / / 18888Copy the code

Switch from base 10 to base 16

string str = Convert.ToString(i, 16);
Copy the code

or

str = i.ToString("X");
Copy the code

If you use a capital X, you get uppercase characters, such as a decimal 12 converted to hexadecimal, an X converted to C, and an X converted to C

You can also use str.toupper () or str.tolower () to convert to uppercase or lowercase

or

str = string.Format("{0:X}", i);
Copy the code

Change from hexadecimal to hexadecimal

i = Convert.ToInt32("A", 16);
Copy the code

If we convert hexadecimal A to base 10, we get 10

or

i = Convert.ToInt32("0xA", 16);
Copy the code

or

i = int.Parse(Convert.ToString(0xA, 10));
Copy the code

Example 1: Set and get the serial number

During network communication, data is usually converted to byte type for transmission

For example, the network communication between the upper computer and the lower computer

When setting the serial number of the lower computer, the upper computer converts the serial number into byte[] and sends it to the lower computer as an instruction parameter

When obtaining the serial number of the lower computer, the upper computer sends instructions to obtain the result and convert byte[] into string

Turn the string byte []

string sn = "1001AAA8801";
byte[] bSn = Encoding.Default.GetBytes(sn);
Copy the code

Turn the string byte []

string reslutSn = Encoding.Default.GetString(bSn);
Copy the code

Example 2 Setting and obtaining a MAC address

Setting a MAC Address

The MAC address format is AA-AA-AA-AA-AA-AA -AA-AA

Win+R Enter CMD and enter getMac to view the MAC address of the computer

For example, the upper computer software sets the MAC address of the lower computer

During the process of setting the MAC address, you need to convert the hexadecimal to hexadecimal byte type after entering the correct MAC address in the text box, and send it to the next computer as a command parameter

string mac = "AA-AA-AA-AA-AA-AA";
string[] macs = mac.Split('-');

byte[] bMac = new byte[6];
for (int j = 0; j < macs.Length; j++)
{
    bMac[j] = (byte)Convert.ToInt32(macs[j], 16);
}
Copy the code

Byte [] can be sent to the lower computer as an instruction parameter

Obtaining a MAC Address

Similarly, if the MAC address is obtained from the lower computer, the data obtained from the lower computer is also of the byte type, and the byte type needs to be converted to hexadecimal

string reslutMac = ""; for (int j = 0; j < bMac.Length; j++) { reslutMac += Convert.ToString((int)bMac[j], 16); if (j ! = bMac.Length - 1) reslutMac += "-"; } reslutMac = reslutMac.ToUpper();Copy the code