1. How to convert a decimal number string into a hexadecimal number string in C #
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. In the serial communication process, it is often used to 16 binary and string, byte array between the conversion
//
private string stringtohexstring (String s,encoding encode)
{
byte[] b = encode. GetBytes (s);//The string is programmed as a byte array by the 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], +);
}
return result;
}
Private String hexstringtostring (string hs, Encoding encode)
{
//divide the string with%, and remove the null character
string[] chars = hs. Split (new char[]{'% '},stringsplitoptions.removeemptyentries);
Byte[] b = new Byte[chars. Length];
//character to 16 byte data
for (int i = 0; i < chars. Length; i++)
{
B[i] = Convert.tobyte (chars[i], +);
}
//To change the byte array to a string
by the specified encoding return encode. GetString (b);
}
///<summary>
///string to 16 binary byte array
///</summary>
///<param name= "hexstring" ></param>
///<returns></returns>
PrivateStaticbyte[] Strtotohexbyte (stringhexstring)
{
hexstring = Hexstring.replace ("","");
if((hexstring.length%2) !=0)
HexString + ="";
byte[] Returnbytes =Newbyte[Hexstring.length/2];
for(inti =0; i < returnbytes.length; i++)
Returnbytes[i] = Convert.tobyte (hexstring.substring (i *2,2), -);
returnReturnbytes;
}
///<summary>
///byte array to 16 binary string
///</summary>
///<param name= "bytes" ></param>
///<returns></returns>
PublicStatic stringBYTETOHEXSTR (byte[] bytes)
{
stringReturnstr ="";
if(Bytes! =NULL)
{
for(inti =0; I < bytes. Length; i++)
{
Returnstr + = Bytes[i]. ToString ("X2");
}
}
returnRETURNSTR;
}
///<summary>
///Convert from Chinese characters to 16 binary
///</summary>
///<param name= "s" ></param>
///<param name= "CharSet" >coding, such as "Utf-8", "gb2312"</param>
/// <param name= "Fenge" >whether each character trailing characters Comma separated</param>
///<returns></returns>
PublicStaticstringTohex (stringSstringCharSetBOOLFenge)
{
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);
stringstr ="";
for(inti =0; I < bytes. Length; i++)
{
str + =string. Format ("{0:x}", Bytes[i]);
if(Fenge && (i! = bytes. Length-1))
{
str + =string. Format ("{0}",",");
}
}
returnStr. ToLower ();
}
///<summary>
///convert from 16 into Chinese
///</summary>
///<param name= "hex" ></param>
///<param name= "CharSet" >coding, such as "Utf-8", "gb2312"</param>
///<returns></returns>
PublicStaticstringUnhex (stringHexstringCharSet
{
if(Hex = =NULL)
ThrowNewArgumentNullException ("Hex");
Hex = hex. Replace (",","");
Hex = hex. Replace ("\ n","");
Hex = hex. Replace ("\\","");
Hex = hex. Replace ("","");
if(Hex. Length%2!=0)
{
Hex + =" -";//Space
}
//you need to convert hex into a byte array.
byte[] bytes =Newbyte[Hex. Length/2];
for(inti =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.
ThrowNewArgumentException ("Hex is not a valid hex number!","Hex");
}
}
System.Text.Encoding CHS = System.Text.Encoding.GetEncoding (charset);
returnChs. GetString (bytes);
}
The above article is reproduced!
This article is from "Cool Little Joe" blog, please be sure to keep this source http://5152481.blog.51cto.com/5142481/1611308
Conversion between C # 's different binaries