Conversion between 2, 10, Hex and ASCII codes in C #

Source: Internet
Author: User
Tags binary to decimal
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

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.