Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. #pragma warning(disable:4996)
  5. #define MAX_LENGTH 100
  6. typedef unsigned int integer_t;
  7.  
  8. char toHex(integer_t num) {
  9. char str[2][4];
  10. int u = 0;
  11. do {
  12. int mod = num % 16;
  13. num = num / 16;
  14.  
  15. if (mod < 10) {
  16. str[1][u] = mod;
  17. }
  18. else {
  19. str[1][u] = 'a' + mod - 10;
  20. }
  21.  
  22.  
  23. mod = num % 16;
  24. num = num / 16;
  25. if (mod < 10) {
  26. str[0][u] = mod;
  27. }
  28. else {
  29. str[0][u] = 'a' + mod - 10;
  30. }
  31. u++;
  32. } while (u<=3);
  33.  
  34. return str;
  35. }
  36.  
  37.  
  38. char toBinary(char num[], int q) {
  39. char binstr[65] = {""};
  40. int n;
  41.  
  42. for (int i = 0;i < Length2(num);i++) {
  43. if (num[i] < 10) {
  44. n = num[i];
  45. }
  46. else {
  47. n = num[i] - 'a' + 10;
  48. }
  49. for (int j = 1; j < 4; j++) {
  50. int mod = num[i] % 2;
  51. num[i] = num[i] / 2;
  52. char d = '0' + mod;
  53. char *strcat(char *binstr, char *d);
  54. }
  55. }
  56.  
  57. return binstr;
  58. }
  59.  
  60.  
  61.  
  62. int Length(integer_t num)
  63. {
  64. int i = 0;
  65. while (num>0){
  66. i++;
  67. num = num / 256;
  68. }
  69. return i;
  70. }
  71.  
  72.  
  73. int Length2(char c[])
  74. {
  75. int i = 0;
  76. while (c[i] != 0)
  77. i++;
  78. return i;
  79. }
  80.  
  81.  
  82.  
  83. int main(void) {
  84. integer_t num = 0;
  85.  
  86. do {
  87. printf("Enter your chislo:\n");
  88. scanf("%ui", &num);
  89.  
  90. // char str[3][6] = { {'1','2','3','4','5','6'}, {'1','2','3','4','5','6'}, {'1','2','3','4','5','6'} };
  91. // char str2[3][6] = { {'1','2','3','4','5','6'}, {'1','2','3','4','5','6'}, {'1','2','3','4','5','3'} };
  92. char str[3][6] = { "" };
  93. char str2[3][6] = { "" };
  94.  
  95.  
  96. //to 16
  97. str[3][6] = toHex(num);
  98.  
  99. int q = Length(num);
  100.  
  101.  
  102. //rev
  103. for (int i = q; i != 0; i--) {
  104. str2[0][i - 1] = str2[0][q - i];
  105. str2[1][i - 1] = str2[1][q - i];
  106. }
  107.  
  108. char strhex1[] = { "0x" };
  109. char strhex2[] = { "0x" };
  110.  
  111. //tostring
  112. for (int i = q; i != 0; i--) {
  113. char d = str[1][i - 1];
  114. char *strcat(char *strhex2, char *d);
  115. char r = str[0][i - 1];
  116. char *strcat(char *strhex2, char *r);
  117. }
  118. for (int i = q; i != 0; i--) {
  119. char t = str2[1][i - 1];
  120. char *strcat(char *strhex1, char *t);
  121. char y = str2[0][i - 1];
  122. char *strcat(char *strhex1, char *y);
  123. }
  124.  
  125. //to 2
  126. char s[33], s2[33];
  127. s[33] = toBinary(strhex1,q);
  128. s2[33] = toBinary(strhex2,q);
  129.  
  130.  
  131.  
  132. int n = Length2(s);
  133. integer_t sum = 0;
  134. for (int i = n - 1;i > -1;i--)
  135. if (s[i] == '1')
  136. sum += pow(2, i);
  137.  
  138.  
  139. //output
  140. printf("%i = %s = %s \n", num, strhex1, s2);
  141. printf("%i = %s = %s \n", sum, strhex2, s);
  142.  
  143.  
  144.  
  145. // printf("The words in your string are reversed, but the separators are located in their place:\n");
  146. // printf("%s\n", str);
  147.  
  148. } while (num != '0');
  149. getchar();
  150.  
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement