C # hexadecimal conversion

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

1) Number System
Binary is used in computers, because binary is easy to operate, easy to implement, and reliable, it provides a favorable way for logical design and saves devices. In order to facilitate the description, it is also commonly used for the abbreviation of binary when the hexadecimal notation is used.

Generally, carry counting is used, which is characterized:
(1) When n goes into one place, N indicates the number of symbols required for a single digit as the base number.
(2) The position representation means that numbers in different positions represent different values, while the unit number represents a definite value at a fixed position, the value on this fixed bit is called the right.
In the computer: D7 D6 D5 D4 D3 D2 D1 D0 only has two types of 0 and 1
8 4 2 1

Ii) Digital Conversion
Conversion principle between different carry counters: the conversion between different carry Counters is based on the principle that two rational numbers, such as equal, must be equal to the integer and fractional part of the two numbers. That is to say, if the first two numbers are equal, they must still be equal after conversion.
Four-digit System
Decimal: 10 Base numbers: 0 ~~ 9. Every 10 times
Binary: There are two bases: 0 ~~ 1. Every second goes into one
Octal: there are eight bases: 0 ~~ 7. Every 8 hours
Hexadecimal: there are 16 base numbers: 0 ~~ 9, a, B, c, d, e, f (a = 10, B = 11, c = 12, D = 13, E = 14, F = 15 ), every 16th one

1. Carry notation of numbers
N = a n-1 * P n-1 + A N-2 * P N-2 +... + A2 * P2 + A1 * P1 + a0 * P0
2. Conversion between decimal number and p-base number
① Convert decimal to binary: convert a decimal integer to a binary integer. Generally, the remainder method is used, while the decimal part is rounded to 2. For example, convert (30) 10 to a binary number.
Convert (30) 10 to binary
2 | 30 .... 0 ---- rightmost
2 15 .... 1
2 7 .... 1
2 3 .... 1
1 .... 1 ---- leftmost
Round (30) 10 = (11110) 2
Convert (30) 10 to hexadecimal number
8 | 30 ...... 6 ------ rightmost
3 ------ leftmost
Round (30) 10 = (36) 8

16 | 30... 14 (e) ---- rightmost
1 ---- leftmost
Hour (30) 10 = (1E) 16
3. Convert the p-base number to the decimal number.
Convert a binary value to decimal format: multiply the last digit of the binary value by 20, and the last and second digits by 21 ,......, Until the maximum bit is multiplied by 2n, then the result of adding the product is its decimal expression.
Convert binary 11110 to decimal
(11110) 2 = 1*24 + 1*23 + 1*22 + 1*21 + 0*20 =
= 16 + 8 + 4 + 2 + 0
= (30) 10

Convert an octal value to decimal format: multiply the last bits of the octal value by 80, and the last and second BITs by 81 ,......, Until the maximum bit is multiplied by 8n, then the result of adding the product is its decimal expression.
Convert octal 36 to decimal
(36) 8 = 3*81 + 6*80 = 24 + 6 = (30) 10
Convert a hexadecimal system to decimal: multiply the last digit of the hexadecimal system by 160, and multiply the last and second digits by 161 ,......, Until the maximum bit is multiplied by 16 N, then the result of adding the product is its decimal expression.
Convert hexadecimal 1E to decimal
(1E) 16 = 1*161 + 14*160 = 16 + 14 = (30) 10
3. Convert binary data to octal data
(1) convert a binary number to an octal number: For integers, each three digits of the binary number are divided into one group from the low to the high. If there are not three digits, add 0 to the left of the high position, replace each three-digit binary number with an octal number. The decimal part starts from the decimal point and is converted from left to right to each three-digit group. For example:
To convert 1101001 of the binary data to 8 bytes
(001 101 001) 2
|
(1 5 1) 8
(1101001) 2 = (151) 8

(2) convert an octal number to a binary number: replace each octal number with a three-digit binary number. For example, convert an octal number (643.503) 8 to a binary number, then
(6 4 3. 5 0 3) 8
|
(110 100 011. 101 000 011) 2
(643.503) 8 = (110100011.101000011) 2
4. Conversion between binary and hexadecimal
(1) convert a binary number to a hexadecimal number: because the power of 2 is equal to 16, according to the binary and octal conversion methods, each four digits of the binary number are represented by a hexadecimal number. The integer part is converted from the right to the left to a group of four decimal points, the decimal part is converted from the decimal point to the right by a group of four digits.
(2) convert hexadecimal to binary
For example, to convert a hexadecimal number to a binary number, you only need to use four binary numbers to represent each hexadecimal number.
For example, to convert (163.5b) 16 to a binary number
(1 6 3. 5 B) 16
|
(0001 0110 0011. 0101 1011) 2
(163.5b) 16 = (101100011.01011011) 2

// Convert decimal to binary
Console. writeline ("binary representation of decimal 166:" + convert. tostring (166, 2 ));
// Convert decimal to octal
Console. writeline ("octal representation of decimal 166:" + convert. tostring (166, 8 ));
// Convert decimal to hexadecimal
Console. writeline ("hexadecimal representation of decimal 166:" + convert. tostring (166, 16 ));

// Convert binary to decimal
Console. writeline ("decimal representation of binary 111101:" + convert. toint32 ("111101", 2 ));
// Octal to decimal
Console. writeline ("octal 44 in decimal format:" + convert. toint32 ("44", 8 ));
// Convert hexadecimal to decimal
Console. writeline ("hexadecimal CC in decimal format:" + convert. toint32 ("cc", 16 ));

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.