C # serial communication, conversion between 16 binary and string, byte array.

Source: Internet
Author: User

1. Converting a string of decimal numbers to a string of hexadecimal numbers

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));

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));

2.conversion between 16 binary and string, byte array

private string Stringtohexstring (string s,encoding encode)
{
Byte[] B = encode. GetBytes (s);//String programming byte array by specified encoding
string result = String. Empty;
for (int i = 0; i < b.length; i++)//byte to 16 characters, separated by%
{
Result + = "%" +convert.tostring (B[i], 16);
}
return result;
}
private string Hexstringtostring (string hs, Encoding encode)
{
Split the string with% and remove the null character
String[] chars = hs. Split (new char[]{'% '},stringsplitoptions.removeemptyentries);
Byte[] B = new Byte[chars. Length];
Character-by-byte changes into 16 bytes of data
for (int i = 0; i < chars. Length; i++)
{
B[i] = Convert.tobyte (chars[i], 16);
}
Converts a byte array into a string by the specified encoding
return encode. GetString (b);
}

3.///<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>
byte array to 16 binary string
</summary>
<param name= "bytes" ></param>
<returns></returns>
public static string bytetohexstr (byte[] bytes)
{
String returnstr = "";
if (bytes! = null)
{
for (int i = 0; i < bytes. Length; i++)
{
Returnstr + = Bytes[i]. ToString ("X2");
}
}
return returnstr;
}

<summary>
Convert from Chinese characters to 16 binary
</summary>
<param name= "S" ></param>
<param name= "CharSet" > Encoding, such as "Utf-8", "gb2312" </param>
<param name= "Fenge" > whether a character trailing characters comma-delimited </param>
<returns></returns>
public static string Tohex (string s, String charset, bool Fenge)
{
if ((s.length% 2)! = 0)
{
s + = "";//Space
throw new ArgumentException ("s is not valid Chinese string!");
}
System.Text.Encoding CHS = System.Text.Encoding.GetEncoding (charset);
byte[] bytes = chs. GetBytes (s);
String str = "";
for (int i = 0; i < bytes. Length; i++)
{
str + = string. Format ("{0:x}", Bytes[i]);
if (Fenge && (i! = bytes. LENGTH-1))
{
str + = string. Format ("{0}", ",");
}
}
Return str. ToLower ();
}

<summary>
Convert from 16 into Chinese
</summary>
<param name= "Hex" ></param>
<param name= "CharSet" > Encoding, such as "Utf-8", "gb2312" </param>
<returns></returns>
public static string Unhex (string hex, String charset)
{
if (hex = = null)
throw new ArgumentNullException ("hex");
Hex = hex. Replace (",", "");
Hex = hex. Replace ("\ n", "");
Hex = hex. Replace ("\ \", "");
Hex = hex. Replace ("", "");
if (hex. Length% 2! = 0)
{
Hex + = "20";//Space
}
You need to convert hex into a byte array.
byte[] bytes = new Byte[hex. LENGTH/2];

for (int i = 0; i < bytes. Length; i++)
{
Try
{
Each of the two characters is a byte.
Bytes[i] = byte. Parse (Hex. Substring (i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
Catch
{
Rethrow an exception with custom message.
throw new ArgumentException ("hex is not a valid hex number!", "hex");
}
}
System.Text.Encoding CHS = System.Text.Encoding.GetEncoding (charset);
Return to CHS. GetString (bytes);
}

C # serial communication, conversion between 16 binary and string, byte array.

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.