C # hexadecimal conversion, operators, and expressions (+-*/% I ++, ++ I,

Source: Internet
Author: User
Tags binary to decimal decimal to binary

C # hexadecimal conversion, operators, and expressions (+-*/% I ++, ++ I,

I. hexadecimal conversion

// Summary:

* When converting from decimal to other hexadecimal values, we use Convert. ToString (decimal number, the hexadecimal value to be converted );

* Convert. ToInt32 is used to Convert from other hexadecimal to decimal (Other hexadecimal strings, other hexadecimal numbers );

* If the conversion is not in decimal format, use Convert. toString (decimal number, in hexadecimal format); and Convert. toInt32 (Other hexadecimal strings, other hexadecimal numbers,

* In other words: for example, convert binary to octal: first convert binary to decimal, and then convert decimal to octal.

Convert binary to hexadecimal, and convert hexadecimal to binary

The same applies to the conversion of octal to octal.

1. // decimal to binary

Stringret = Convert. ToString (96, 2 );

Console. WriteLine (ret );

2. // binary to decimal

IntretNum = Convert. ToInt32 ("1100000", 2 );//

Console. WriteLine (retNum );

3. // decimal to octal

Ret = Convert. ToString (96, 8 );

Console. WriteLine (ret );

4. // octal to decimal

RetNum = Convert. ToInt32 ("140", 8 );//

Console. WriteLine (retNum );

5. // decimal to hexadecimal

Ret = Convert. ToString (96, 16 );

Console. WriteLine (ret );

6. // hexadecimal to decimal

RetNum = Convert. ToInt32 ("60", 16 );//

Console. WriteLine (retNum );

7. // convert binary data to 8 digits

RetNum = Convert. ToInt32 ("1100000", 2); // binary to decimal

Ret = Convert. ToString (retNum, 8); // decimal to octal

Console. WriteLine (ret );

Ii. Operators and expressions

1. // calculate the right and save the result on the right in the variable.

① Intret = 12 + 15;

Console. WriteLine (ret );

② Ret = 12-15;

Console. WriteLine (ret );

Note the following: * Division summary:

* Integers/integers get integers. If they are not fully divided, the operators are obtained.

* Float-Type Floating Point Numbers/integers get float-Type Floating Point Numbers

* Double-Type Floating-point numbers/integers get double-Type Floating-point numbers

③ Ret = 9/3;

Console. WriteLine (ret );

④ // The integer is divided by the integer. If division is not allowed, the calculation result will be the operator of the two integers.

Ret = 18/4;

Console. WriteLine (ret); // The output result is 4.

// ** Note that the result is correct. Divide the integer by the integer. It can only be a quotient integer.

FloatretFloat = 18/4;

Console. WriteLine (retFloat); // The output result is also 4

⑤ // Divide the decimal number by an integer. The float format must be f

RetFloat = 12.56f/4;

Console. WriteLine (retFloat );

⑥ // The double Type below is also the output decimal point

DoubleretDouble = 12.56/4;

Console. WriteLine (retDouble );

7. ret = 3*9;

Console. WriteLine (ret );

Returns // the remainder of the touch operation.

Ret = 18% 4;

Console. WriteLine (ret );

2,

* Conclusion: I ++ uses the I value before I = I + 1;

* ++ I is the first operation to perform I = I + 1, and then use the value of I

// The following lines of code (1) have the same effect as those of (2), but the output results are also different.

① Console. WriteLine ("---------");

Intnum1 = 10;

Num1 ++; // num1 = num1 + 1;

Console. WriteLine (num1 );

Console. WriteLine ("------**---");

Intnum2 = 10;

+ + Num2; // num2 = num2 + 1;

Console. WriteLine (num2 );

② // The following lines of code differentiate between ++ and before and after, and the output results are different

Console. WriteLine ("*******");

Intnum3 = 10;

// Print the value of num3 before addition.

Console. WriteLine (num3 ++); // output result 10;

Console. WriteLine ("num3 = {0}", num3); // output result 11

Intnum4 = 10;

// First perform addition and then print the value

Console. WriteLine (++ num4); // output result 11

③ Easy to differentiate:

A: intnum5 = 10;

// The following code is equivalent to Console. WriteLine (num5 ++ );

Console. WriteLine (num5); // 10

Num5 = num5 + 1;

B: intnum6 = 10;

// The following code is equivalent to Console. WriteLine (++ num6 );

Num6 = num6 + 1;

Console. WriteLine (num6 );

3. Operation exercises

// Enter a total number of seconds from the console, and the time, minute, and second will be displayed,

// Example

// Enter 86496

// Output 24-hour 1 minute 36 seconds

Console. WriteLine ("Enter the number of seconds ");

Inttime = int. Parse (Console. ReadLine ());

Inthours = time/(60*60 );

Intseconds = time % 60;

Intminute = time % (60*60)/60; // first divided by 3600, it is calculated that it is not enough for an hour, and then divided by 60

Console. WriteLine ("{0} hour {1} minute {2} seconds", hours, minute, seconds );

Related Article

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.