In programming, hexadecimal conversion

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

In programming, hexadecimal conversion

(Binary, octal, decimal, hexadecimal)

Binary is composed of 0 and 1

[Decimal to binary] For example, if 23 is written as binary 10111

23/2 = 11 + 1

11/2 = 5 1

5/2 = 2 1

2/2 = 1 0

Therefore, the binary value for 23 is 10111.

[Binary to decimal formula]

A * 2 ^ 0 + B * 2 ^ 1 + ...... + M * 2 ^ (n-1) =

For example, 1011 is written in decimal format as 1*2 ^ 0 + 1*2 ^ 1 + 0*2 ^ 2 + 1*2 ^ 3 = 11.

Key points: 1. When writing binary to decimal, write from right to left

2. The index starts from 0.

[Decimal to octal]

For example

48/8 = 6 + 0

Therefore, the octal ratio of 48 is 60.

 

[Octal to decimal] (convert binary to decimal)

A * 8 ^ 0 + B * 8 ^ 1 + ...... + M * 8 ^ (n-1) =

The decimal value of 60 is 0*8 ^ 0 + 6*8 ^ 1 = 48

 

Hexadecimal numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A (10), B (11), C (12), D (13), E (14 ), F (15)

[Convert to hexadecimal in decimal format]: for example, the hexadecimal format of 45 is 2D.

[Hexadecimal to decimal]: a * 16 ^ 0 + B * 16 ^ 1 + ...... + M * 16 ^ (n-1) =

 

[Binary to octal] (Binary from left to right, each three groups, each group from right to the first is 1, the second is 2, the second is 4, respectively in the binary number multiplication, the sum of each group is obtained .) {Why are three groups, because the three sides of 2 are 8. (1, 2, 4 )}

For example, 10110101

10 110 101

0*1 + 1*2 = 2 0*1 + 1*2 + 1*4 = 6 1*1 + 0*2 + 1*4 = 5

Therefore, the octal ratio of 10110101 is 562.

 

[Octal to binary] (divide each number by 2, and add 0 to the left for each group with less than three digits. Write the order from left to right)

For example, the binary value of 72 is 111010.

 

Convert binary to hexadecimal]

Same as above. From left to right, each of the four numbers is a group, and the number multiplied IS (1, 2, 4, 8)

 

[Hexadecimal to binary]

Same as above, but note (ABCDEF) that a group of letters cannot be separated by 2, and the final result is written from left to right.


Implementation of hexadecimal conversion using C language programming

Input a decimal number N and convert it to the R-base number output. Here we assume the maximum value is hexadecimal.
# Include <stdio. h>
Int exchange (int)
{
If (a = 10) {printf ("A"); return 0 ;}
If (a = 11) {printf ("B"); return 0 ;}
If (a = 12) {printf ("C"); return 0 ;}
If (a = 13) {printf ("D"); return 0 ;}
If (a = 14) {printf ("E"); return 0 ;}
If (a = 15) {printf ("F"); return 0 ;}
Printf ("% d", );
}
Int main (int argc, char * argv [])
{
Int x, y, I, j, a [1, 110];
While (scanf ("% d", & x, & y) = 2) // enter x, y, x in decimal format, and y in decimal format.
{
If (x <0) {printf ("-"); x =-x;} // if x is less than zero, convert it to a positive number before calculation, and then output the negative number first.
If (x = 0) {printf ("0"); return 0 ;}
For (I = 0; x! = 0; I ++) // the basic rule for converting to another base in decimal format
{
A [I] = x % y;
X = x/y;
}
I --;
If (y> 10) // if the hexadecimal value to be converted is greater than the decimal value, special processing is required.
For (j = I; j> = 0; j --)
Exchange (a [j]);
Else for (j = I; j> = 0; j --)
Printf ("% d", a [j]);
Printf ("\ n ");
}
Return 0;
}

Question about hexadecimal conversion in programming

The essence of BCD code is to use four binary numbers to represent a decimal number, so 021127212901 is: 0000 0010 0001 0001 0010 0111 0010 0001 0010 1001 0000 0001. Because the 8-bit binary number is a byte, The hexadecimal number in the byte format is 02 11 27 21 29 01. Therefore, it is easy to convert. That is:

Unsigned char int2bcd (unsigned char n)
{
Return (n/10) * 16 + n % 10;
}

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.