Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- char * decToBin(long long x)
- {
- char *number = (char *)malloc(100);
- int i = 0;
- do
- {
- number[i++] = x % 2 + '0';
- x = x / 2;
- } while (x != 0);
- number[i] = '\0';
- int numberLength = strlen(number) - 2;
- int m = numberLength / 2;
- for (int i = 0; i <= m; i++)
- {
- char tmp = number[i];
- number[i] = number[numberLength - i + 1];
- number[numberLength - i + 1] = tmp;
- }
- return number;
- }
- char* splitOctets(char* number, char splitter)
- {
- int newIndex = 1;
- char* splitStr = (char *)malloc(125);
- splitStr[0] = splitter;
- int notSignificantZerosCount = ((strlen(number) % 4) == 0)? 0: 4 - (strlen(number) % 4);
- int i = notSignificantZerosCount;
- while(i > 0)
- {
- splitStr[newIndex] = '0';
- newIndex++;
- i--;
- }
- for(i = 0; i <= strlen(number); i++)
- {
- if ((i + notSignificantZerosCount) != 0 && (i + notSignificantZerosCount) % 4 == 0)
- {
- splitStr[newIndex] = splitter;
- newIndex++;
- }
- splitStr[newIndex] = number[i];
- newIndex++;
- }
- return splitStr;
- }
- int main()
- {
- long long x;
- scanf("%lli", &x);
- char *t = splitOctets(decToBin(x), '|');
- puts(t);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement