Skip to content

Instantly share code, notes, and snippets.

@halivert
Last active April 11, 2021 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halivert/bf628204dfee9df6d4ba0aeadf74087b to your computer and use it in GitHub Desktop.
Save halivert/bf628204dfee9df6d4ba0aeadf74087b to your computer and use it in GitHub Desktop.
C utilities
#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;
}
#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