Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdbool.h>
- #include<stdlib.h>
- int baseToInt(char* str, int base);
- int mypow(int n, int k);
- void main()
- {
- int base;
- char *str;
- char* value;
- printf("**************************************************************\n");
- printf("Select input format (2-binary, 8-octal, 10-decimal, 16 hexadecimal, 0-exit):");
- scanf("%d", &base);
- str = (char*)malloc(32 * sizeof(char));
- if (base != 16)
- {
- printf("\nEnter value in the specified format:");
- if (str == NULL)
- printf("Not enough memory\n"); // if string null print not enough and finish.
- scanf("%s", str);
- }
- if (base == 16)
- {
- printf("\nEnter value in the specified format:");
- if (str == NULL)
- printf("Not enough memory\n"); // if string null print not enough and finish.
- scanf("%s", str + '\0');
- }
- value = baseToInt(str,base);
- printf("\nEntered value is : %d",value);
- free(str);
- }
- int baseToInt(char* str, int base)
- {
- char *num;
- num = (char*)malloc(32 * sizeof(char));
- if (base == 2)
- {
- if (str == 0)
- num = 0;
- num = *str % 2 + 10 * (baseToInt(*str / 2, base));
- }
- if (base == 8)
- {
- if (str == 0)
- num == 0;
- num = *str % 8;
- *str /= 8;
- baseToInt(*str, base);
- }
- if (base == 10)
- {
- num = str;
- }
- if (base == 16)
- {
- }
- return num;
- }
- /*
- int mypow(int n,int k)
- {
- if (k == 0)
- return 1;
- return power(n, k - 1) * n;
- }
- */
- /*
- **************************************************************
- Select input format (2-binary, 8-octal, 10-decimal, 16 hexadecimal, 0-exit):
- Enter value in the specified format:
- Entered value is:
- Binary form:
- Octal form:
- Decimal form:
- Hexadecimal form:
- FINISH
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement