Integer Conversion Program

Source: Internet
Author: User

1. Brief Introduction

In fact, decimal places also have hexadecimal conversion. This article mainly describes the implementation of the hexadecimal conversion code for integer types. The function prototype is as follows:
Char * ChangeFromDecimal (unsigned int value, int base );
// Value: A 10-digit numeric value; base: the number of converted hexadecimal values; return value: the number of converted hexadecimal values, and add '\ 0'
Int ChangeToDecimal (char * value, int base );
// Opposite to ChangeFromDecimal, convert a certain hexadecimal string to an int value.

2. Code Implementation

# Include <iostream>
# Include <stack>
Using namespace std;

Char * ChangeFromDecimal (unsigned int value, int base ){
Char * result;
Stack <char> result_stack;
Unsigned int tmp;

Result_stack.push ('\ 0 ');
While (value ){
Tmp = value % base;
Value = value/base;
Result_stack.push (tmp <10? (Tmp + '0'): (tmp-10 + 'A '));
}
Result = new char [result_stack.size ()];
Tmp = 0;
While (! Result_stack.empty ()){
Result [tmp ++] = result_stack.top ();
Result_stack.pop ();
}
Return result;
}

Int ChangeToDecimal (char * value, int base ){
Int result = 0;
While (* value! = '\ 0 '){
Result = result * base;
Result + = (* value) <= '9 '? (* Value-'0') :( * value-'A' + 10 );
Value ++;
}
Return result;
}

Int main (){
Int num = 253;
Char * result;
Cout <(result = ChangeFromDecimal (num, 2) <endl;
Cout <ChangeToDecimal (result, 2) <endl;
Cout <"----------------------------------------" <endl;
Delete [] result;
Cout <(result = ChangeFromDecimal (num, 8) <endl;
Cout <ChangeToDecimal (result, 8) <endl;
Cout <"----------------------------------------" <endl;
Delete [] result;
Cout <(result = ChangeFromDecimal (num, 16) <endl;
Cout <ChangeToDecimal (result, 16) <endl;
Delete [] result;
Result = 0;
System ("PAUSE ");
Return 0;
}

3. Highlights

· Numbers greater than or equal to 10 and numbers smaller than 10 must be processed separately, especially the ASCII codes of '0'-'9' and 'A'-'Z' are not consecutive.
· If you use parameters instead of return values, char * should be used for char * &, and int * should be used for int *.
· Given the limited number of English letters, there is a limit on the base. 10 digits plus 26 letters can be expressed at most in 36 hexadecimal notation.
· The most important thing is that when the first function converts an int into a string, it must be interpreted as an unsigned int, because the function uses the remainder operation. If it is not explained, negative Numbers are displayed incorrectly after calculation. However, the second function does not need to return unsigned int. It can also return int, because only the multiplication and addition of values are supported. Of course, using unsigned int is actually the safest and safest method. If unsigned int is used, the first function will be directly converted into an unsigned int. If the return value of the second function is forcibly converted to an int, the result can be reasonably displayed.

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.