### Thread: Reverse process of decimal to binary in C?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2017
Posts
17
Rep Power
0

#### Reverse process of decimal to binary in C?

Hi,

In Encoding side, i have converted the Hardware decimal values into binary values.

Code:
```#include <stdio.h>
int convertDecimalToBinary()
{

int temperature=26;
int v_x1_u32=25;
int v_pressure_u32=152383;
int  array1[5]={0};
int  array2[5]={0};
int  array3[18]={0};
int merge[102]={0};
int *ai;
int b,i,j,k,l,m,n,p,q,size;

for (i = 0; i<5; i++)
{
array1[4 - i] = (temperature >> i) & 1;

}
for (j = 0; j<5; j++)
{

array2[4 - j] = (v_x1_u32 >> j) & 1;

}

for (m = 0; m<18; m++)
{
array3[17 - m] = (v_pressure_u32 >> m) & 1;

}

for(k=2,size=0;k<7,size<5;k++,size++)
{
merge[k]=array1[size];
//printf("%d",merge[k]);
}

merge[8]=1;
for(i=0, l=9; l<14 && i<5; i++, l++)
{
merge[l]=array2[i];

}
//for(m=0;m<14;m++)
//printf("%d",merge[m]);
merge[15]=1;

for(p=0, q=17; q<35 && p<18; p++, q++)
{
merge[q]=array3[p];

}
ai= (int *)(merge);

printf("\n");
for(b=0;b<102;b++)
{
printf("%d",merge[b]);
}
}
int main()
{
convertDecimalToBinary();
}```
Now i switched to decoding side..How above C codes converted into decimal values ? Logic please.

same like
Code:
```            temperature=26;
v_x1_u32=25;
v_pressure_u32=152383;```
2. Perhaps we have trouble with your concept because the data is always binary within the digital computer. What varies is how you choose to represent it. Here's an example of a union.
Code:
```#include<stdio.h>

union {
unsigned char u;
char c;
} both;

int main() {
int i;
for (i = 48; i < 128; i += 6) {
both.u = i;
printf("%3i%5c\n", both.u, both.c);
}
return 0;
}

#if 0
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Wed Nov  1 13:12:21

a=./c && make -k \$a && \$a
cc -Wall -g c.c -lgomp  -o c
48    0
54    6
60    <
66    B
72    H
78    N
84    T
90    Z
96    `
102    f
108    l
114    r
120    x
126    ~

Compilation finished at Wed Nov  1 13:12:21
#endif```