Character conversion (, hexadecimal, ASCII)

Source: Internet
Author: User

Lists some of the operations used, including hexadecimal conversion and ASCII conversion.

I. Let's talk about the conversion between 2, 8, 10, and 16 hexadecimal formats.

The system. Convert class provides comprehensive conversion functions between various types and values:

Several common methods

1, convert. toint32 (string value, int frombase): converts a string to a number (for example, 2-digit 0010> 2 ^ 1 = 2)

frombase: Base (2, 8, 10, 16) of the parameter as the name suggests

2, convert. tostring ( int value, int tobase ): converts a number to a string.

tobase: target base

  

1.12-digit (string) >>> 10-digit (INT)

String strbase2 = "0101 ";

Int intbase10 = convert. toint32 (strbase2,2);

Result: 5

1.28-digit (string) >>> 10-digit (INT)

String strbase8 = "0101 ";

Int intbase10 = convert. toint32 (strbase8,8);

Result: 65

1.3Hexadecimal (string) >>> 10 hexadecimal (INT)

String strbase16 = "0101 ";

Int intbase10 = convert. toint32 (strbase16,16);

Result: 257

1.410 hexadecimal (INT) >>> 2 hexadecimal (string)

Int intbase10 = 9; (we know that the result is 1001)

String strbase2 = convert. tostring (intbase10,2);

 Result: 1001

1.510-digit (INT) >>> 8-digit (string)

// Int intbase10 = 9; (the result is 11, but it cannot be 8 hexadecimal. If it is changed to 15, the result is> 8 ^ 1 + 7)

Int intbase10 = 15;

String strbase8 = convert. tostring (intbase10,8);

Result: 17

1.610 hexadecimal (INT) >>> 16 hexadecimal (string)

// Int intbase10 = 9, change to 27> 16 ^ 1 + 11 (that is, B ))

Int intbase10 = 27;

String strbase16 = convert. tostring (intbase10,16);

Result: 1B.

1.7Conversion of other types(For example, 2> 8> 16> 16> 2)

my idea is to first use convert. toint32 converts a string to an intermediate int, and then uses convert. tostring converts an int to a string in the target format

example: convert a hexadecimal value to a binary value

string base16 =" AB "; (we know that the number of digits in the result must be a multiple of 4)

int temp = convert. toint32 (base16, 16 );

String strbase2 = convert. tostring (temp,2);

Result: 10101011

For example, convert a hexadecimal value to an octal value.

string base16 =" AB "; (we know that the number of digits in the result must be a multiple of 3)

int temp = convert. toint32 (base16, 16 );

String strbase8 = convert. tostring (temp,8);

Result: 253

PS: we know that in practice, a hexadecimal system is defined, not necessarily a string.

In this way: int base16 =0xAB; (prefix with 0x)

In this case, refer to the above example:

We can write it as int temp = convert. toint32 (base16); (to be simple, not)

Result: 253

Ii. ASCII Conversion

2.1ASCII string to decimal number
String TR = "water ";
Int II = (INT) convert. tochar (TR ));

2.2ASCII string to hexadecimal number

String S2 = "hydrostatic"; (two Chinese characters, that is, four characters)
Byte [] BA = (system. Text.) asciiencoding. Default. getbytes (S2); (ANSI is the default)

Foreach (VAR s in BA)
{
Console. writeline (s );
}

Result: 190

178

203

174

2.3Convert to an ASCII string in hexadecimal notation

Int T = 97; (corresponding to "")

String S = (char) T). tostring ();

Result:

Int T = 12345678;

String S = (char) T). tostring ();

What are the results? I am going. Who knows this...

Result: be cautious .....

PS:

At this time, we have a question:

Reference 2.2: The result is as follows: 190 178 203 174. How can we convert these int types to "hydrostatic"

We use system. Text. encoding. default.Getbytes ()Get a byte array. You can also use getstring to convert the array to the corresponding character.

(Of course note :)

String SSS = system. Text. encoding. default.Getstring(New byte [] {190,178 });

The SSS result is "static"

System. Text. encoding .(???), Here's ??? There are many types (default> ANSI, UTF-8, Unicode...), just as needed

Reprinted: http://www.cnblogs.com/jingshui_rwb/archive/2011/06/28/2092224.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.