当前位置:网站首页>Conversion between hexadecimal numbers

Conversion between hexadecimal numbers

2022-04-23 17:05:00 begeneral

1、 Decimal to binary

Decimal to binary has 2 Ways of planting , The first is to divide the decimal number by 2, The remainder until the end is 0 perhaps 1, Then arrange the remainder of the last result from high to low , If the number of digits is not enough , High compensation 0.

Like decimal numbers 9:

9/2=4 more than 1

4/2=2 more than 0

2/2=1 more than 0

So the binary is :0000 1001. This is a mention , Because the divisor is 2, So the result of the last division can only be 0 perhaps 1, be equal to 0 The case is that the divisor is 1 perhaps 0, That is to say 1/2 or 0/2, Other decimal numbers and 2 The end of division

The result is 1.

This method only applies when the decimal number is small , When decimal numbers are large , Except it's not very convenient . At this time, you can first convert the decimal system to hexadecimal , Then convert hexadecimal to binary , This method will be introduced below .

2、 Decimal to hexadecimal

The principle of decimal to hexadecimal is the same as that of decimal to binary , Is to divide this decimal number by 16, Arrange the remainder from high to low .

Like decimal numbers 100:

100/16=6 more than 4

because 6 Less than 16 You don't have to go any further except , The result is 0x64. Let's look at a bigger number , Decimal number :5000

5000/16=312 more than 8

312/16=19 more than 8

19/16=1 more than 3

So the result is 0x1388.

3、 Hexadecimal to decimal

In hexadecimal A、B、C、D、E、F Decimal 10、11、12、13、14、15

This conversion is the method of converting decimal to hexadecimal, and vice versa , Multiply the number in the hexadecimal digit by the corresponding 16 To the power of , such as :0x3E

0x3E=3*16^1+14*16^0=3*16+14*1=48+14=62

Another example is the need to 2 Hexadecimal number in bytes :0x36BC

0x36BC=3*16^3+6*16^2+11*16^1+12*16^0=3*4096+6*256+11*16+12*1=14012

4、 Hex to binary

Every number in hexadecimal is a percentage of 4 A bit of , Convert this number into 4 Bit binary number can convert a hexadecimal number into binary .

such as :0x5F

5=4+1=2^2+2^0=0101

F=8+4+2+1=2^3+2^2+2^1+2^0=1111

So the result is 0101 1111

Just now it was said that when converting a large decimal number into binary , First convert it to hexadecimal , Then convert hexadecimal to decimal .

Because divided by 16 It's definitely better than dividing by 2 The number of times to be less , And it is more convenient to convert hexadecimal to binary , So I think it will be more efficient .

 

 

 

 

 

 

版权声明
本文为[begeneral]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554082018.html