Marc
Marc

Reputation: 73

Convert a decimal number saved in an array into a binary number saved in an array, and vice versa

I've been researching ways to convert a decimal number to a binary number. However in the examples I find the decimal number to be converted is not within an array. For example:

void decToBinary(int n) 
{ 
    // array to store binary number 
    int binaryNum[32]; 

    // counter for binary array 
    int i = 0; 
    while (n > 0) { 

        // storing remainder in binary array 
        binaryNum[i] = n % 2; 
        n = n / 2; 
        i++; 
    } 
}

In my case, however, I have a decimal number saved in an array uint8_t* (randomly generated) how could you pass it to a binary number saved in an array? And then, how could I pass it back to decimal number saved in an array?

Upvotes: 2

Views: 140

Answers (1)

Hitokiri
Hitokiri

Reputation: 3699

You should return the binaryNum because it's local variable. But firstly, you have to revert the array for exact binary number:

uint8_t * returArray;
returArray = malloc(sizeof(int) * i);

for(int j = i - 1, k = 0; j >= 0; j--, k++) {
    returArray[j] = binaryNum[k];
}

The function becomes as:

uint8_t * decToBinary(int n, int *size) 
{ 
    // array to store binary number 
    int binaryNum[32];
    uint8_t * returArray;

    // counter for binary array 
    int i = 0; 
    while (n > 0) { 

        // storing remainder in binary array 
        binaryNum[i] = n % 2; 
        n = n / 2; 
        i++; 
    } 

    returArray = malloc(sizeof(int) * i);
    if (!returArray)
       // handle error;

    for(int j = i - 1, k = 0; j >= 0; j--, k++) {
        returArray[j] = binaryNum[k];
    }
    *size = i;
    return returArray;
}

size value is the size of binary array.

The main for test:

int main(void) {
    int size;
    uint8_t * a = decToBinary(25, &size);
    printf("size = %d\n", size);
    for (int i = 0; i < size; ++i)
    {
        printf("%d", a[i]);
    }
    free(a);
    return 0;
}

If you want to convert back to decimal, you need to do something invert the way you convert decimal to binary:

int main() {
   ...
   int decimal = 0;
   int base = 1;
   for (int i = size-1; i >= 0; i--)
   {
       decimal += base * a[i];
       base *= 2;
   }
}

Update for converting all value in array:

uint8_t * converAll(int * decimalArr, int size, int *new_size) {
    uint8_t * returArray = malloc(sizeof(uint8_t));
    if(! returArray) {
        // handle error
    }
    int bsize;
    int temp = 0, k = 0;
    for (int i = 0; i < size; ++i)
    {
       uint8_t *arr = decToBinary(decimalArr[i], &bsize);
       returArray = realloc(returArray ,sizeof(uint8_t) * (bsize + temp));
       if(! returArray) {
        // handle error
       }
       for (int j = 0; j < bsize; j++) {
            returArray[k] = arr[j];
            k++;
       }
       temp += bsize;
    }
    *new_size = k;

    return returArray;
}

This function can be used as:

int arr[] = {3,6,3,8,9,4};
uint8_t * a = converAll(arr, 6,  &size);

Upvotes: 1

Related Questions