C ++ numeric Conversion

Source: Internet
Author: User

1. Convert decimal to n hexadecimal

1 <n <= 9

The conversion function is implemented as follows:

 

// Convert decimal dec to n, and the result is stored in other. other allocates memory externally <br/> void Decimal_to_Other (int dec, char * other, int n) <br/> {<br/> // 1 <n <= 9 <br/> int I = 0; <br/> int dec_copy = dec; <br/> char * tmpArray = new char [64]; <br/> while (dec_copy! = 0) {<br/> tmpArray [I] = dec_copy % n + '0'; <br/> dec_copy/= n; <br/> I ++; <br/>}// the high position of the native string is stored in tmpArray. <br/> // For example, 6. the binary value is saved as tmpArray [0 ~ 2] = {0, 1} <br/> int len = I; // n hexadecimal String Length <br/> for (I = 0; I <len; I ++) {// The tmpArray will be reversed below, exist in other <br/> other [I] = tmpArray [len-i-1]; <br/>}// other [0 ~ 2] = {, 0} This is the result <br/> other [len] = '/0'; <br/> delete [] tmpArray; <br/>}

 

The call is as follows:

CString szNumber; <br/> char * bin = new char [64]; // convert to binary <br/> Decimal_to_Other (m_Decimal, bin, 2 ); <br/> szNumber. format ("% s", bin); <br/> AfxMessageBox (szNumber); </p> <p> char * octal = new char [64]; // convert to octal sequence <br/> Decimal_to_Other (m_Decimal, octal, 8); <br/> szNumber. format ("% s", octal); <br/> AfxMessageBox (szNumber); </p> <p>

 

Ii. Convert decimal to hexadecimal

The conversion function is as follows:

// Hex allocate space externally <br/> void Decimal_to_Hex (int dec, char * hex) <br/>{< br/> int I = 0, flag; <br/> int dec_copy = dec; <br/> char * tmpArray = new char [64]; <br/> while (dec_copy! = 0) {<br/> flag = dec_copy % 16; <br/> switch (flag) {<br/> case 10: <br/> tmpArray [I] = 'a'; <br/> break; <br/> case 11: <br/> tmpArray [I] = 'B '; <br/> break; <br/> case 12: <br/> tmpArray [I] = 'C'; <br/> break; <br/> case 13: <br/> tmpArray [I] = 'D'; <br/> break; <br/> case 14: <br/> tmpArray [I] = 'E '; <br/> break; <br/> case 15: <br/> tmpArray [I] = 'F'; <br/> break; <br/> default: <br/> tmpArray [I] = flag + '0'; <br/> break; <br/>}< br/> dec_copy/= 16; <br/> I ++; <br/>}< br/> int len = I; // n hexadecimal String Length <br/> for (I = 0; I <len; I ++) {<br/> hex [I] = tmpArray [len-i-1]; <br/>}< br/> hex [len] = '/0'; <br/> delete [] tmpArray; <br/>}

 

The call is as follows:

CString szNumber; <br/> char * hex = new char [64]; // convert to hexadecimal format <br/> Decimal_to_Hex (m_Decimal, hex ); <br/> szNumber. format ("% s", hex); <br/> AfxMessageBox (szNumber );

 

 

Iii. Convert binary to decimal

 

Int Binary_to_Decimal (CString szBin) {<br/> int len = szBin. getLength (); // the length of a binary string <br/> char * str = new char [len]; // the length of a string <br/> double result = 0.0; // result </p> <p> strcpy (str, szBin. getBuffer (0); </p> <p> for (int I = 0; I <len; I ++) {<br/> if (str [I] = '1') {<br/> result + = pow (2, len-i-1 ); // calculate the len-I power of 2 and add it to the final result <br/> continue; <br/>}< br/> else if (str [I] = '0 ') {</p> <p >}< br/> else {<br/> AfxMessageBox ("the input binary string is incorrect! "); <Br/> return 0; <br/>}< br/> return (int) result; <br/>}

 

The call is as follows:

CString szBinary = "101101101"; <br/> int decimal; <br/> CString szDecimal; <br/> decimal = Binary_to_Decimal (szBinary); <br/> szDecimal. format ("decimal result % d", decimal); <br/> AfxMessageBox (szDecimal );

 

Iv. Convert octal to decimal

Int Octal_to_Decimal (CString szOct) {<br/> int len = szOct. getLength (); // String Length <br/> char * str = new char [len]; // storage string <br/> double result = 0.0; // result </p> <p> int nOct; <br/> strcpy (str, szOct. getBuffer (0); </p> <p> for (int I = 0; I <len; I ++) {</p> <p> if (str [I] = '0') {<br/> nOct = 0; <br/>}< br/> else if (str [I] = '1') {<br/> nOct = 1; <br/>}< br/> else if (str [I] = '2') {<br/> nOct = 2; <br/>}< br/> else if (str [I] = '3') {<br/> nOct = 3; <br/>}< br/> else if (str [I] = '4 ') {<br/> nOct = 4; <br/>}< br/> else if (str [I] = '5') {<br/> nOct = 5; <br/>}< br/> else if (str [I] = '6') {<br/> nOct = 6; <br/>}< br/> else if (str [I] = '7') {<br/> nOct = 7; <br/>}< br/> else {<br/> AfxMessageBox ("the eight-character string entered is incorrect! "); <Br/> return 0; <br/>}</p> <p> result + = (nOct * pow (8, len-i-1 )); <br/> // CString msg; msg. format ("nOct = % d pow = % f result = % f", nOct, pow (8, len-i-1), result); <br/> // AfxMessageBox (msg ); <br/>}< br/> // delete [] str; <br/> return (int) result; <br/>}

The call is as follows:

CString szOctal = "1721"; <br/> int decimal; <br/> CString szDecimal; <br/> decimal = Octal_to_Decimal (szOctal); <br/> szDecimal. format ("decimal result % d", decimal); <br/> AfxMessageBox (szDecimal );

 

 

5. Convert hexadecimal to decimal

Int Hex_to_Decimal (CString szHex) {<br/> int len = szHex. getLength (); // String Length <br/> char * str = new char [len]; // storage string <br/> double result = 0.0; // result </p> <p> int nHex; <br/> strcpy (str, szHex. getBuffer (0); </p> <p> for (int I = 0; I <len; I ++) {</p> <p> if (str [I] = '0') {<br/> nHex = 0; <br/>}< br/> else if (str [I] = '1') {<br/> nHex = 1; <br/>}< br/> else if (str [I] = '2') {<br/> nHex = 2; <br/>}< br/> else if (str [I] = '3') {<br/> nHex = 3; <br/>}< br/> else if (str [I] = '4 ') {<br/> nHex = 4; <br/>}< br/> else if (str [I] = '5') {<br/> nHex = 5; <br/>}< br/> else if (str [I] = '6') {<br/> nHex = 6; <br/>}< br/> else if (str [I] = '7') {<br/> nHex = 7; <br/>}< br/> else if (str [I] = '8') {<br/> nHex = 8; <br/>}< br/> else if (str [I] = '9') {<br/> nHex = 9; <br/>}< br/> else if (str [I] = 'A') {<br/> nHex = 10; <br/>}< br/> else if (s Tr [I] = 'B') {<br/> nHex = 11; <br/>}< br/> else if (str [I] = 'C') {<br/> nHex = 12; <br/>}< br/> else if (str [I] = 'D') {<br/> nHex = 13; <br/>}< br/> else if (str [I] = 'E') {<br/> nHex = 14; <br/>}< br/> else if (str [I] = 'F') {<br/> nHex = 15; <br/>}< br/> else {<br/> AfxMessageBox ("The hexadecimal string entered is incorrect! "); <Br/> return 0; <br/>}</p> <p> result + = (nHex * pow (16, len-i-1 )); <br/>}< br/> return (int) result; <br/>}

 

The call is as follows:

CString szHex = "1A2B"; <br/> int decimal; <br/> CString szDecimal; <br/> decimal = Hex_to_Decimal (szHex); <br/> szDecimal. format ("decimal result % d", decimal); <br/> AfxMessageBox (szDecimal );

 

6. stack implementation

// Data structure C language version (Yan Weimin) P48 <br/> void conversion (int N) {<br/> InistStack (S ); <br/> scanf ("% d", dec); // read a decimal value from the screen <br/> while (dec! = 0) {// conversion <br/> Push (S, dec % N); <br/> dec/= N; <br/>}< br/> while (! StackEmpty (S) {// display result <br/> Pop (S, e); <br/> printf ("% d", e ); <br/>}< br/>}

 

 

PS: I have implemented a numeric conversion tool. You can download and use it.

Http://download.csdn.net/source/2869264

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.