Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- short int convertIn (const char *number, char baseIn) {
- /*find length of number*/
- stringl = 0;
- total = 0;
- for (i=0; number[i] != '\0'; i++){
- stringl++;
- }
- /* set the base */
- if (baseIn == 'd'){
- base = 10;
- }
- if (baseIn == 'o'){
- base = 8;
- }
- if (baseIn == 'x'){
- base = 16;
- }
- if (baseIn == 'b'){
- base = 2;
- }
- for (i=0; number[i] != '\0'; i++){
- if (number[i] == '-'){
- continue;
- }
- if (number[i] == '0'){
- continue;
- }
- subtotal = base;
- j = (stringl-1-i);
- while (j>1){
- if ((32767/subtotal)<base){
- printf("Input number too large to handle.");
- exit(0);
- }
- subtotal = subtotal*base;
- j--;
- }
- if (j==0){
- subtotal = 1;
- }
- if (number[i] > '@'){
- numeric = (number[i] - 55);
- }
- if (number[i] < '@'){
- numeric = (number[i] - 48);
- }
- if ((32767/subtotal)<numeric){
- printf("Input number too large to handle.");
- exit(0);
- }
- subtotal = numeric*subtotal;
- if ((32767-total)<subtotal){
- printf("Input number too large to handle.");
- exit(0);
- }
- total = total + subtotal;
- }
- if (number[0] == '-'){
- total = total*(-1);
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement