1. Simple conversion of the hexadecimal value of the communication protocol
Binary goto Decimal
Console.WriteLine ("Decimal Representation of binary 111101:" +convert.toint32 ("111101", 2));
Octal goto Decimal
Console.WriteLine ("Decimal Representation of octal 44:" +convert.toint32 ("44", 8));
Hexadecimal goto Decimal
Console.WriteLine ("Decimal representation of the hexadecimal CC:" +convert.toint32 ("CC", 16)); /* more commonly used */
Decimal Turn binary
Console.WriteLine ("Decimal 166 binary representation:" +convert.tostring (166, 2));
Decimal Turn octal
Console.WriteLine ("Octal Representation of decimal 166:" +convert.tostring (166, 8));
Decimal Turn hex
Console.WriteLine ("Hexadecimal Representation of decimal 166:" +convert.tostring (166, 16));
2.16 binary format conversions are often used for communication strings
<summary>
Decimal to 8-bit binary
</summary>
<param name= "source" > Sources decimal Data </param>
<returns> eight-bit binary number </returns>
public static string get10turn8bits (int source)
{
String _2bitstringsource = convert.tostring (source, 2);
String _2bitstringres = "";
for (int i = 0; i < 8-_2bitstringsource.length; i++)
{
0 less than 8 people
_2bitstringres + = "0";
}
_2bitstringres = _2bitstringres + _2bitstringsource;
return _2bitstringres;
}
<summary>
String Turn Hex
</summary>
<param name= "str" > Raw string data: FF FF 08</param>
<returns> return hex byte array </returns>
public static byte[] Gethexvalue (String str)
{
string[] Sourcestr = str. Split (');
byte[] Coverthexbyte = new Byte[sourcestr.length];
Try
{
for (int i = 0; i < sourcestr.length; i++)
{
Coverthexbyte[i] = (byte) (int. Parse (Sourcestr[i], System.Globalization.NumberStyles.HexNumber));;
}
}
Catch
{ }
return coverthexbyte;
}
<summary>
Converts 4-bit hexadecimal to 5-bit decimal number
</summary>
<returns> five-bit decimal number </returns>
static public string Convert5bitdec (string inputstr)
{
String outputstr = "";
Try
{
for (int i = 0; i < inputstr.length; i = i + 4)
{
Outputstr + = Convert.ToInt32 (Inputstr.substring (i, 4), 16). ToString ("00000");
}
}
catch (Exception) {}
return outputstr;
}
<summary>
String to 16 binary byte array
</summary>
<param name= "HexString" ></param>
<returns></returns>
private static byte[] Strtotohexbyte (string hexstring)
{
hexstring = Hexstring.replace ("", "");
if ((hexstring.length% 2)! = 0)
HexString + = "";
byte[] returnbytes = new BYTE[HEXSTRING.LENGTH/2];
for (int i = 0; i < returnbytes.length; i++)
Returnbytes[i] = Convert.tobyte (hexstring.substring (i * 2, 2), 16);
return returnbytes;
}
<summary>
Convert two-bit ASCII (16 mechanism of one byte) to decimal number
</summary>
<param name= "Inputstr" ></param>
<returns></returns>
public static string _2bitasciiconvertdec (String inputstr)
{
String outputstr = "";
Try
{
for (int i = 0; i < inputstr.length; i = i + 2)
{
Outputstr + = (char) convert.toint32 (inputstr.substring (i,2), 16);
}
}
catch (Exception) {}
return outputstr;
}
C # serial communication of 16 bytes commonly used in byte conversion