Byte[] Array and hexadecimal strings are converted to and from strings (reproduced)

Source: Internet
Author: User

1. How can I convert a string of decimal digits into a string of hexadecimal digits in C #

Decimal conversion Binary
Console.WriteLine ("Decimal 166 binary representation:" +convert.tostring (166, 2));
Decimal Turn octal
Console.WriteLine ("Octal of the Decimal 166" said: "+convert.tostring (166, 8));"
Decimal turn hexadecimal
Console.WriteLine ("Hexadecimal Representation of decimal 166:" +convert.tostring (166, 16));

Binary Turn Decimal
Console.WriteLine ("Decimal Representation of binary 111101:" +convert.toint32 ("111101", 2));
Octal decimal System
Console.WriteLine ("Decimal representation of the octal 44:" +convert.toint32 ("44", 8));
Hexadecimal decimal
Console.WriteLine ("Decimal Representation of hexadecimal CC:" +convert.toint32 ("CC", 16));

2. In the serial communication process, often need to use the 16 binary and string, byte array of conversion between
//

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


<summary>
string 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 16 Feed 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>
Converting from Chinese to 16
</summary>
<param name= "S" ></param>
<param name= "CharSet" > Code, such as "Utf-8", "gb2312" </param>
<param name= "Fenge" > whether each character used 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 to Chinese characters
</summary>
<param name= "Hex" ></param>
<param name= "CharSet" > Code, 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 to a byte array.
byte[] bytes = new Byte[hex. LENGTH/2];

for (int i = 0; i < bytes. Length; i++)
{
Try
{
Every 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 CHS. GetString (bytes);
}

The above article for reprint!

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.