The symbol table for the binary conversion is [0-9a-za-z], a total of 61 characters and a maximum of 62 binary.
The idea is that the original system is first converted to 10, then converted to the target system.
Questions:
For negative numbers, a small partner says that you can discard the symbols directly, carry them in integers, and finally back up the minus sign, which I think is wrong.
The correct approach is to consider whether to either 16-bit (short) or 32-bit (int) or 64-bit (long long), the first to find the twos complement (at this time the positive negative is unified), the binary number is converted to decimal after the conversion to another binary ( If a small partner knows how to convert the binary directly into any of the methods you can leave a message to tell me, I appreciate it. Note that the arbitrary system here is not pure 8 16 and so on 2 of the power of the system, there are other such as 7, 9, etc.).
The following is a code that I think is less appropriate for dealing with negative numbers :
Input format: original, target binary, number in the original system (represented by string)
Output format: The number in the target system
1#include <iostream>2#include <string>3#include <cmath>4 using namespacestd;5 6 //converts any character to decimal, where A-Z represents 10-35,a-z 36-61 and is adjusted with the corresponding ASCII code .7 Long LongConverttodec (Charc)8 {9 Long LongDecnum;Ten if(c>='a'&& c<='Z') Onedecnum=c- the; A Else if(c>='A'&& c<='Z') -decnum=c- in; - Else if(c>='0'&& c<='9') thedecnum=c- -; - - returnDecnum; - } + - //convert decimal to these characters + CharConverttodec (Long Longc) A { at Long LongObjchar; - if(c>=Ten&& c<= *) -objchar=c+ the; - Else if(c>= $&& c<= A) -objchar=c+ in; - Else if(c>=0&& c<=9) inobjchar=c+ -; - to returnObjchar; + } - the intMain () * { $ intsrc;Panax Notoginseng intobj; - stringnum; the + while(cin>>src>>obj>>num) A { the + BOOLIsnegative=false; - if(num[0]=='-') $ { $Num.erase (0); -Isnegative=true; - } the - Long Longdecnum=0;//decimal number (middle number)Wuyi for(Long LongI=0; I<num.size (); + +i) theDecnum+=converttodec (Num[i]) *pow (Src,num.size ()-1-i); - Wu stringstrtmp; - Long Longtmp; About while(decnum>0) $ { -Tmp=decnum%obj; -STRTMP=CONVERTTODEC (TMP) +strtmp; -Decnum/=obj; A } + the if(isnegative) -strtmp='-'+strtmp; $cout<<strtmp<<Endl; the } the the return 0; the}
Also hope to advise.
2~62 bit arbitrary binary conversion (C + +)