2 ~ 62-bit arbitrary hexadecimal conversion (c ++), 62-bit hexadecimal

Source: Internet
Author: User

2 ~ 62-bit arbitrary hexadecimal conversion (c ++), 62-bit hexadecimal

The hexadecimal conversion symbol table is [0-9a-zA-Z], with a total of 61 characters. The maximum value is 62.

The idea is to convert the original base to base 10 and then to the target base.

Question:

For negative numbers, some friends say they can discard the symbols directly, perform carry conversion according to integers, and then add the negative signs back. I think this is wrong.

The correct method is: consider 16-bit (short), 32-bit (int), or 64-bit (long), first obtain the binary complement code (then the positive and negative numbers are unified ), convert the binary number to decimal and convert it to other hexadecimal values. (If a friend knows how to directly convert the binary number to any hexadecimal value, leave a message to tell me. I am very grateful. Note that the arbitrary hexadecimal format here is not simply an 8-16 or a 2-power hexadecimal format, and there are others such as 7 and 9 ).

 

The following is the code that I think is not suitable for dealing with negative numbers:

Input Format: Base, destination, number in base (represented by a string)

Output Format: numbers in the base-to-destination format

1 # include <iostream> 2 # include <string> 3 # include <cmath> 4 using namespace std; 5 6 // convert any character to decimal, where a-z represents 10-35, A-Z represents 36-61, with the corresponding ASCII code adjusted 7 long convertToDec (char c) 8 {9 long decNum; 10 if (c> = 'A' & c <= 'Z') 11 decNum = c-87; 12 else if (c> = 'A' & c <= 'Z') 13 decNum = C-29; 14 else if (c> = '0' & c <= '9') 15 decNum = c-48; 16 17 return decNum; 18} 19 20 // convert decimal to these characters 21 char convertToDec (long c) 22 {23 long objchar; 24 if (c> = 10 & c <= 35) 25 objchar = c + 87; 26 else if (c> = 36 & c <= 61) 27 objchar = c + 29; 28 else if (c> = 0 & c <= 9) 29 objchar = c + 48; 30 31 return objchar; 32} 33 34 int main () 35 {36 int src; 37 int obj; 38 string num; 39 40 while (cin> src> obj> num) 41 {42 43 bool IsNegative = false; 44 if (num [0] = '-') 45 {46 num. erase (0); 47 IsNegative = true; 48} 49 50 long decNum = 0; // decimal number (intermediate number) 51 for (long I = 0; I <num. size (); ++ I) 52 decNum + = convertToDec (num [I]) * pow (src, num. size ()-1-i); 53 54 string strTmp; 55 long tmp; 56 while (decNum> 0) 57 {58 tmp = decNum % obj; 59 strTmp = convertToDec (tmp) + strTmp; 60 decNum/= obj; 61} 62 63 if (IsNegative) 64 strTmp = '-' + strTmp; 65 cout <strTmp <endl; 66} 67 68 return 0; 69}

 

Hope you can advise me.

 

Related Article

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.