C # serial communication of 16 bytes commonly used in byte conversion

Source: Internet
Author: User

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

Related Article

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.