Browse Source

added function to convert int from 0 - 3 into binary string

remotes/origin/conversionOfNumbers
Laurin 11 months ago
parent
commit
7b11525191
  1. 19
      src/conversionOfNumbers/conversionOfNumbers.c

19
src/conversionOfNumbers/conversionOfNumbers.c

@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
char* convertIntToBinaryStr(int input){
if(input > 3 || input < 0) return NULL;
int length = 2;
int rest;
char* result = (char*)malloc(sizeof(char) * length + 1);
int index = length - 1;
do{
rest = input % 2;
input /= 2;
if(rest == 1) result[index] = '1';
if(rest == 0) result[index] = '0';
index--;
}while(input != 0);
result[length] = '\0';
return result;
}
Loading…
Cancel
Save