Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int SymbolCodeToInt(char c, int b)
  5. {
  6. if (b <= 10 && c >= '0' && c <= '0' + b - 1) // cifra in baza b
  7. {
  8. c -= '0'; // se obtine cifra
  9. }
  10. else if (b <= 16 && c >= 'A' && c <= 'A' + b - 1) // cifra hexazecimala scrisa cu litera mare
  11. {
  12. c += 10 - 'A';
  13. }
  14. else if (b <= 16 && c >= 'a' && c <= 'a' + b - 1) // cifra hexazecimala scrisa cu litera mica
  15. {
  16. c += 10 - 'a';
  17. }
  18. return c;
  19. }
  20.  
  21. unsigned long long StrToInt(const char string[], int nS, int b)
  22. {
  23. int j;
  24. unsigned long long I = 0;
  25.  
  26. for (j = 0; j < nS; ++j) // parcurgem fiecare caracter din sirul de intrare
  27. {
  28. // transformam caracterul in valoare, depinzand de pozitia in codul ASCII
  29. // apoi folosim formula din laborator
  30. I = I * b + SymbolCodeToInt(string[j], b);
  31. }
  32.  
  33. return I;
  34. }
  35.  
  36. char IntToSymbolCode(int i, int b)
  37. {
  38. if (i <= 9 && i >= 0) // cifra in baza 10
  39. {
  40. return (char)i + '0'; // se obtine cifra
  41. }
  42. else if (i >= 10) // cifra hexazecimala
  43. {
  44. return (char)i - 10 + 'A';
  45. }
  46. return '?';
  47. }
  48.  
  49. void Reverse(char S[], int nS) // Little Endian => Big Endian
  50. {
  51. int j = 0;
  52. int k = nS - 1;
  53. char aux;
  54. while (j < k)
  55. {
  56. aux = S[j];
  57. S[j] = S[k];
  58. S[k] = aux;
  59. j++;
  60. k--;
  61. }
  62. }
  63.  
  64. int IntToStr(unsigned long long I, int b, char S[])
  65. {
  66. int nS = 0; // numarul de caractere ale sirului format
  67. do {
  68. S[nS++] = IntToSymbolCode((int)(I % b), b);
  69. I /= b;
  70. } while (I != 0);
  71.  
  72. S[nS] = '\0';
  73. Reverse(S, nS);
  74. return nS;
  75. }
  76.  
  77. void Converteste(const char numar[], size_t nrCifre, int bazaInitiala, int bazaFinala, char rezultat[])
  78. {
  79. IntToStr(StrToInt(numar, (int)nrCifre, bazaInitiala), bazaFinala, rezultat);
  80. }
  81.  
  82. int main() {
  83. char numar[100];
  84. char rezultat[100];
  85. int bI, bF;
  86.  
  87. printf("Introduceti numarul ca sir de caractere: ");
  88. fgets(numar, 99, stdin);
  89. numar[strlen(numar) - 1] = '\0';
  90.  
  91. printf("Introduceti baza de numeratie in care este numarul %s: ", numar);
  92. scanf("%d", &bI);
  93.  
  94. printf("Introduceti baza de numeratie in care sa fie convertit numarul %s: ", numar);
  95. scanf("%d", &bF);
  96.  
  97. Converteste(numar, strlen(numar), bI, bF, rezultat);
  98. printf("Numarul %s din baza %d in baza %d este: %s\n", numar, bI, bF, rezultat);
  99.  
  100. Converteste(rezultat, strlen(rezultat), bF, bI, numar);
  101. printf("Numarul %s convertit inapoi, din baza %d in baza %d este: %s\n", rezultat, bF, bI, numar);
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement