Last active
April 11, 2021 17:14
-
-
Save halivert/bf628204dfee9df6d4ba0aeadf74087b to your computer and use it in GitHub Desktop.
C utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
char *intToBin(int num) { | |
int bits = (num == 0); | |
int cp = num; | |
for (cp = num; cp > 0; cp >>= 1) bits++; | |
char *result = (char *)malloc(bits * sizeof(char)); | |
for (cp = num; cp > 0; cp >>= 1) result[--bits] = (cp & 1) + '0'; | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
ssize_t readLine(char *str) { | |
char *buffer = NULL; | |
size_t bufferSize; | |
ssize_t tam; | |
if (-1 == (tam = getline(&buffer, &bufferSize, stdin))) { | |
return tam; | |
} | |
strcpy(str, buffer); | |
free(buffer); | |
str[tam - 1] = '\0'; | |
return tam - 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment