Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.  
  5. char * decToBin(long long x)
  6. {
  7.     char *number = (char *)malloc(100);
  8.     int i = 0;
  9.     do
  10.     {
  11.         number[i++] = x % 2 + '0';
  12.         x = x / 2;
  13.     } while (x != 0);
  14.     number[i] = '\0';
  15.  
  16.     int numberLength = strlen(number) - 2;
  17.     int m = numberLength / 2;
  18.     for (int i = 0; i <= m; i++)
  19.     {
  20.         char tmp = number[i];
  21.         number[i] = number[numberLength - i + 1];
  22.         number[numberLength - i + 1] = tmp;
  23.     }
  24.     return number;
  25. }
  26.  
  27. char* splitOctets(char* number, char splitter)
  28. {
  29.     int newIndex = 1;
  30.     char* splitStr = (char *)malloc(125);
  31.     splitStr[0] = splitter;
  32.     int notSignificantZerosCount = ((strlen(number) % 4) == 0)? 0: 4 - (strlen(number) % 4);
  33.     int i = notSignificantZerosCount;
  34.     while(i > 0)
  35.     {
  36.         splitStr[newIndex] = '0';
  37.         newIndex++;
  38.         i--;
  39.     }
  40.     for(i = 0; i <= strlen(number); i++)
  41.     {
  42.         if ((i + notSignificantZerosCount) != 0 && (i + notSignificantZerosCount) % 4 == 0)
  43.         {
  44.             splitStr[newIndex] = splitter;
  45.             newIndex++;
  46.         }
  47.         splitStr[newIndex] = number[i];
  48.         newIndex++;
  49.     }
  50.     return splitStr;
  51. }
  52.  
  53. int main()
  54. {
  55.     long long x;
  56.     scanf("%lli", &x);
  57.     char *t = splitOctets(decToBin(x), '|');
  58.     puts(t);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement