Conversion between 2, 10, Hex and ASCII codes in C #
-------- // Conversion of various character types in C #, used for serial communication ------------
// Understanding of character Length
String S = "I Am a 2 soldier ";
Int Len = S. length; // 6 Characters
Byte [] Sarr = system. Text. encoding. Default. getbytes (s );
Len = Sarr. length; // 11 bytes
// It does not make sense to convert the string to a 10-digit notation. It must be converted to the corresponding ASCII code.
Int T1 = 81;
String S1 = t1.tostring ();
// This 10-digit conversion corresponds to the ASCII character.
S1 = (char) T1). tostring ();
// Hexadecimal string: this is meaningless. It is tostring.
Int intab = 0x16;
S1 = intab. tostring ();
// Hexadecimal conversion corresponds to ASCII characters:
Byte babb = 0x45;
String ass = (char) babb). tostring ();
// Convert an ASCII string to a decimal number
String TR = "2AB Liu ";
String d = "";
For (INT I = 0; I <tr. length; I ++)
{
Int II = (INT) convert. tochar (tr. substring (I, 1 ));
D = d + "" + II. tostring ();
}
// Convert ASCII string to hexadecimal number
String S2 = "2AB Liu ";
Byte [] BA = system. Text. asciiencoding. Default. getbytes (S2 );
Stringbuilder sb = new stringbuilder ();
Foreach (byte B in BA)
{
SB. append (B. tostring ("X") + "");
}
// Convert the hexadecimal number to the hexadecimal number.
Int INTA = 0x16; // It must contain 0x
String stra = "16"; // The string can be left blank
Int inta1 = convert. toint32 (INTA );
Int inta2 = convert. toint32 (stra, 16 );
// Convert hexadecimal to hexadecimal
Stra = convert. tostring (inta2, 16 );
// Convert the hexadecimal value to the binary value. The hexadecimal value is similar to the hexadecimal value.
Int int10 = 10;
String str2 = convert. tostring (int10, 2 );
// Convert binary to decimal.
Int10 = convert. toint32 (str2, 2 );
To:
Http://www.cnblogs.com/luhuan860/archive/2009/07/02/1515510.html
-------- // Conversion of various character types in C #, used for serial communication ------------
// Understanding of character Length
String S = "I Am a 2 soldier ";
Int Len = S. length; // 6 Characters
Byte [] Sarr = system. Text. encoding. Default. getbytes (s );
Len = Sarr. length; // 11 bytes
// It does not make sense to convert the string to a 10-digit notation. It must be converted to the corresponding ASCII code.
Int T1 = 81;
String S1 = t1.tostring ();
// This 10-digit conversion corresponds to the ASCII character.
S1 = (char) T1). tostring ();
// Hexadecimal string: this is meaningless. It is tostring.
Int intab = 0x16;
S1 = intab. tostring ();
// Hexadecimal conversion corresponds to ASCII characters:
Byte babb = 0x45;
String ass = (char) babb). tostring ();
// Convert an ASCII string to a decimal number
String TR = "2AB Liu ";
String d = "";
For (INT I = 0; I <tr. length; I ++)
{
Int II = (INT) convert. tochar (tr. substring (I, 1 ));
D = d + "" + II. tostring ();
}
// Convert ASCII string to hexadecimal number
String S2 = "2AB Liu ";
Byte [] BA = system. Text. asciiencoding. Default. getbytes (S2 );
Stringbuilder sb = new stringbuilder ();
Foreach (byte B in BA)
{
SB. append (B. tostring ("X") + "");
}
// Convert the hexadecimal number to the hexadecimal number.
Int INTA = 0x16; // It must contain 0x
String stra = "16"; // The string can be left blank
Int inta1 = convert. toint32 (INTA );
Int inta2 = convert. toint32 (stra, 16 );
// Convert hexadecimal to hexadecimal
Stra = convert. tostring (inta2, 16 );
// Convert the hexadecimal value to the binary value. The hexadecimal value is similar to the hexadecimal value.
Int int10 = 10;
String str2 = convert. tostring (int10, 2 );
// Convert binary to decimal.
Int10 = convert. toint32 (str2, 2 );
To:
Http://www.cnblogs.com/luhuan860/archive/2009/07/02/1515510.html