Convert binary to hexadecimal

Source: Internet
Author: User

convert binary to hexadecimal

1. basic principle: Because the hexadecimal number base is the four power of 2, a binary is converted to a hexadecimal number. If it is an integer, from its low position to the high position, every four digits constitute a group, and then each group is The number corresponding to the number is expressed in hexadecimal notation. If there is a decimal part, the group is calculated from the decimal point to the left and right sides according to the method described.

(11010111100010111) 2 = (3af17) 16

original:

http://www.chinatax.gov.cn/jypx/jsjjczs/zp_2to16.htm

2. binary-hexadecimal relationship

binary system 0000 0001 0010 0011 0100 0101 0110

hexadecimal 0 1 2 3 4 5 6 7

binary system 1000 1001 1010 1011 1100 1101 1110

hexadecimal 8 9 A (10) B (11) C (12) D (13) E (14) f (15)

the binary number of four digits can be used to represent a hexadecimal value. For example, if the binary number of (3A) 16 is:

3 is 0011, A is 1010, and merge to 00111010. Remove the leftmost 0 value (111010) 2

right: Convert binary to hexadecimal, you only need to separate the digits of the binary from the left to the left, and compare the units with the hexadecimal values.

3. example 1

file * DEST = fopen (" aa.txt ", "R +"); // write a hexadecimal file

file * src = fopen (". CPP "," R "); // Read File

while (CH = GETC (SRC ))! = EOF)

{

fprintf (DEST," % 2x ", CH );

}

4. Example 2

Char * bin2hex (char * SRC, unsigned int size)

{

Char Dest [100] = {0 };

If (sizeof (DEST)/2 <size)

{

Size = sizeof (DEST)/2;

}

For (INT I = 0; I <size; I ++)

{

Sprintf (& Dest [I * 2], "% 2x", (unsigned char) SRC [I]);

}

Return DEST;

}

original:

http://zhidao.baidu.com/question/185562563.html

More details

http://www.cnblogs.com/lds85930/archive/2007/09/19/897912.html

Http://bbs.csdn.net/topics/270017393

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.