[Code example] A simple unsigned integer hexadecimal converter implemented in C ++, a hexadecimal Converter

Source: Internet
Author: User

[Code example] A simple unsigned integer hexadecimal converter implemented in C ++, a hexadecimal Converter

Hello everyone, what we bring to you today is a self-implemented simple hexadecimal converter written in C ++, which is used for the 10-hexadecimal number and the 8-hexadecimal number and the 16-hexadecimal number, conversion of binary numbers.

First, describe what is hexadecimal. n-base is a method used to represent a value. n-base, as the name suggests, returns n to 1. in our daily life, we use a base-10 number, which goes every 10 to 1. The modern computer processor can only process a base-2 number, although it seems that the former Soviet Union has tried to develop a 10-digit computer, it is of course the end.

The reason why a computer uses a binary system is that it is easy to implement and has only two code elements, 0 and 1, which correspond to the two states of some things in the natural world (for example, the switch is enabled and disconnected, level ). in our records and compilation languages, we often use hexadecimal numbers to facilitate writing, because a hexadecimal number corresponds to four binary numbers. an 8-digit number corresponds to three 2-digit numbers, while a 10-digit number corresponds to four 2-digit numbers, and there are 6 escape codes. in advanced programming languages, binary, octal, and hexadecimal are prefixed with 0b, 0, and 0x, respectively. For example, 0xB2 represents 10 decimal number 178. in assembly languages, B, O, and H are used as suffixes. For example, 30 H Represents the decimal number 48 and is also the ASCII code of the character '0.

The key to implementing this applet isNumber Conversion Algorithm. The unsigned integer is in decimal format,Expand by rightYou can; from decimal to any hexadecimal, useShort DivisionReturns the remainder until the quotient is zero.

Short division example. The value of decimal number 53 corresponds to the binary number 0b110101.

The project consists of the custom header file convertfuncs. h and the source program file main. cpp. The Code is as follows:

Convertfuncs. h:

1 # ifndef CONVERTFUNCS_H_INCLUDED 2 # define CONVERTFUNCS_H_INCLUDED // includes alert 3 4 # include <iostream> 5 6 using namespace std; 7 8 const unsigned limit = (unsigned) (sizeof (unsigned) * 8*0.3 + 1); // limits the maximum number of digits that can be processed in a 10-digit number, which is determined by the machine implementation. 9 10 string deciToHex (unsigned deci) {// hexadecimal to hexadecimal functions 11 12 string hexStr (0.75 * limit, ''); // reserved space for the target string, the hexadecimal number corresponds to 3/4 of the 10 hexadecimal number, and the string constructor 13 int Value = 0 is called; // Value stores the remainder of each short Division 14 int I = 0; 15 16 if (deci <10) // when the number of to-be-converted values is less than 10, the hexadecimal notation of 16 and 10 is the same as 17 return string (1, (char) deci ); 18 19 for (; deci! = 0; ++ I, deci/= 16) {// short division loop 20 Value = deci % 16; 21 switch (Value) {// multiple branch selection indicates 10 ~ 15 letters 22 case 10: hexStr. at (I) = 'a'; break; 23 case 11: hexStr. at (I) = 'B'; break; 24 case 12: hexStr. at (I) = 'C'; break; 25 case 13: hexStr. at (I) = 'D'; break; 26 case 14: hexStr. at (I) = 'E'; break; 27 case 15: hexStr. at (I) = 'F'; break; 28 default: hexStr. at (I) = Value + '0'; // the number to be converted to the corresponding character 29} 30} 31 hexStr = hexStr. substr (0, I); // obtain the character string 32 33 reverse (hexStr. begin (), hexStr. end (); // use the iterator to reverse the string because it is written 34 35 return hexStr; // return the corresponding hexadecimal number string 36} 37 38 string deciToOct (unsigned deci) {// 10 hexadecimal to octal functions, with a structure similar to 39 40 string hexStr (limit, ''); 41 int Value = 0; 42 int I = 0; 43 44 if (deci <8) 45 return string (1, (char) deci); 46 47 for (; deci! = 0; ++ I, deci/= 8) {48 Value = deci % 8; 49 hexStr. at (I) = Value + '0'; 50} 51 52 hexStr = hexStr. substr (0, I); 53 54 reverse (hexStr. begin (), hexStr. end (); 55 56 return hexStr; 57} 58 59 string deciToBin (unsigned deci) {// 10 hexadecimal to 2 hexadecimal function, the structure is similar to 60 61 string hexStr (3 * limit, ''); 62 int Value = 0; 63 int I = 0; 64 65 for (; deci! = 0; ++ I, deci/= 2) {66 Value = deci % 2; 67 hexStr. at (I) = Value + '0'; 68} 69 70 hexStr = hexStr. substr (0, I); 71 72 reverse (hexStr. begin (), hexStr. end (); 73 74 return hexStr; 75} 76 77 long anyToDeci (string any, unsigned scale) {// expand function 78 79 long sum = 0 by weight; // sum is cumulative and 80 int n = any. length (); // use the string class method to obtain the string length 81 82 for (int I = 0; I <n; I ++) 83 if (any. at (I)> = '0' & any. at (I) <= '9') 84 sum + = (any. at (I)-'0') * pow (scale, n-1-i); // Power Multiplication and accumulation 85 else if (any. at (I)> = 'A' & any. at (I) <= 'F') // process the number of hexadecimal letters. 86 sum + = (any. at (I)-'A' + 10) * pow (scale, n-1-i); 87 else88 sum + = (any. at (I)-'A' + 10) * pow (scale, n-1-i); 89 return sum; 90} 91 92 # endif // convertfuncs_h_encoded DED

Limit is the maximum number of digits in the input decimal number. if sizeof (unsigned) is 4 on a machine, that is, the unsigned integer occupies 4 bytes, that is, 32 bits. The maximum number is 232. According to 210 ≈ 103, there are 220 ≈ 106,230, therefore, 232 is represented by at least 10 decimal digits. Therefore, the input decimal number can be at most 10 digits.

 

Main. cpp:

1 # include <iostream> 2 # include <algorithm> // reverse function declaration 3 # include "convertfuncs. h "// custom header file 4 5 using namespace std; 6 7 int main () {8 9 system (" color 3F "); // set the background color and foreground color of the console window to 10 11 int k = 0; 12 unsigned deci = 0; // input the hexadecimal number 13 extern string deciToHex (unsigned ); 14 extern string deciToOct (unsigned); 15 extern string deciToBin (unsigned); 16 extern long anyToDeci (string, unsigned); // function prototype 17 string any = ""; // reserved Null String 18 unsigned scale = 0; // hexadecimal ID 19 20 cout <"unsigned integer converter" <endl; 21 cout <"By Alexios Yan" <endl; 22 cout <endl; 23 24 while (true) {// infinite loop function menu 25 cin. sync (); // clear the input buffer 26 cout <"press any key to continue" <endl; 27 cin. get (); 28 system ("cls"); // clear screen 29 cout <"function list:" <endl; 30 cout <"0. exit "<endl; 31 cout <" 1. 10 to 16 "<endl; 32 cout <" 2. 10 to 8 "<endl; 33 cout <" 3. 10 to 2 "<endl; 34 cout <" 4. 2, 8, or 16 to 10 "<endl; 35 cout <endl; 36 cout <" select: "<endl; 37 cin> k; 38 39 switch (k) {// call different functions as needed 40 case 0: exit (0); 41 break; 42 case 1: cout <"Enter the number of 10 hexadecimal values to be converted (no more than" <limit-1 <"bit):" <endl; 43 cin> deci; 44 cout <"the corresponding hexadecimal number is 0x" <deciToHex (deci) <endl; 45 cout <endl; 46 break; 47 case 2: cout <"Enter the number of 10 hexadecimal values to be converted (no more than" <limit-1 <"bit):" <endl; 48 cin> deci; 49 cout <"the corresponding octal number is 0" <deciToOct (deci) <endl; 50 cout <endl; 51 break; 52 case 3: cout <"Enter the number of 10 hexadecimal values to be converted (no more than" <limit-1 <"bit):" <endl; 53 cin> deci; 54 cout <"the corresponding binary number is 0b" <deciToBin (deci) <endl; 55 cout <endl; 56 break; 57 case 4: cout <"Enter the number to be converted to a decimal number:" <endl; 58 cin> any; 59 cout <"Enter the number in hexadecimal format: "<endl; 60 cin> scale; 61 cout <" Result: "<anyToDeci (any, scale) <endl; 62 cout <endl; 63 break; 64 default: cout <"incorrect selection. Please reselect" <endl; 65 cout <endl; 66 break; 67} 68} 69 return 0; 70}

This project is compiled and run successfully under the GCC compiler. Variables cannot be defined in the switch block in the GCC compiler.

Run:

 

 

 

 

 

Thank you! Please indicate the source for reprinting. Thank you for your cooperation!

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.