Hello, been trying to work out my code. but somehow it doesnt work at certain parts.
can't use math.h by the way.
#include <stdio.h>
#include <conio.h>
int main(void)
{
int input,ans,i,total,j;
int hex[16]={0},counter=0,hexadecimal1=0,hexadecimal2=0,hexadecimal3=0,hexadecimal4=0;
char value,value2,value3,value4;
char hexadecimal[17]={0,0,0,0,0,0,0,0,0,0,'A','B','C','D','E','F'};
printf("enter value:");
scanf("%d",&input);
while(input>0){
i=1;
ans=input%10000;
input/=10000;
while(ans>0){
total=ans%10;
ans=ans/=10;
j=total*i;
i=i*2;
hex[counter]=hex[counter]+j;
counter++;
}
}
for(counter=0;counter<4;counter++)
hexadecimal1+=hex[counter];
for(counter=4;counter<8;counter++)
hexadecimal2+=hex[counter];
for(counter=8;counter<12;counter++)
hexadecimal3+=hex[counter];
for(counter=12;counter<16;counter++)
hexadecimal4+=hex[counter];
value=hexadecimal[hexadecimal1];
value2=hexadecimal[hexadecimal2];
value3=hexadecimal[hexadecimal3];
value4=hexadecimal[hexadecimal4];
printf("Value is:");
if(value4==0)
printf("%d",hexadecimal4);
else
printf("%c",value4);
if(value3==0)
printf("%d",hexadecimal3);
else
printf("%c",value3);
if(value2==0)
printf("%d",hexadecimal2);
else
printf("%c",value2);
if(value==0)
printf("%d",hexadecimal1);
else
printf("%c",value);
getch();
}
thought of converting the binary no. to decimal then comparing the value to a char array filled with hexadecimal values. but i have no idea how to make the array support integer values and char values..