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..

Is your binary to decimal conversion correct ?

For decimal to hexa decimal use the approach mentioned here

its kinda correct. i can read up to 10 numbers, if more than that it will turn all values to 0.

No, don't convert to decimal. It's not necessary, and it's harder. You can convert directly into hex.

Finish defining your char hexadecimal[17] array (and it only needs 16 values). Then start at the end of your binary and convert 4 'bits' into an integer. Use that integer to get the hex character. Ex:
You enter 101100101
you convert 0101 into it's binary value (5) then load the hex character into your hex-array answer.

so i should move my hex array into the loop and save it under a variable?
is there a way for an array to support both integer and char values?

What?!?

the code is ok if you input about 8-9 binary values. but when i enter the 10th value, it turns the whole thing to 0. not sure why tho

Your program only inputs 1 binary value. You can't get to 8-9 binary values without a loop that contains the input.

i mean the no. of digits. if i input 10 numbers it will turn the last value to 0

Oh, 10 binary digits....

Add output statements at key points in the program to see what values are being set into what variables. When you see a value you don't expect, that's going to be a problem. Fix it and try again.

you can have:

char hexadecimal[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

but your code is inefficient at the least. Don't read the binary number with scanf("%d"), it will convert to an integer.
You should:
1 - read the string
2 - check that's it only made of '0' and '1'
3 - get its length (strlen)
4 - group the char by packets of 4, by taking special care of the first one which may be of size 1, 2, 3 or 4 depending on the input binary number
5 - compute the decimal number represented by the 4 binary digits
6 - use the array hexadecimal to output the correct char based on the input

eg:
input: 10000110100101
becomes: 10 0001 1010 0101
then: 2 1 10 5
serving as indexes in the hexadecimal array to give: 21A5

i see. so if i use %d it can only get 10 no.s from the user right?
by scanning it as a string i have to convert it into int or will it be already considered an int data type?

This whole code sequence is way too complicated. Here are some suggestions:

1) Don't use the %d format in scanf. Read the data in as a character string,

2) All characters in the input string will be either '0' or '1'. Use the characteristics of the character values to your advantage. Subtract '0' from each character and you will be left with int 0 or 1.

3) You have a choice to make here. You can start at the end of the input string (the LSB) and work backwards, or at the beginning of the string (MSB) and work forwards. Each has it's advantages. Starting with the MSB is simpler logically. I'll illustrate this here In this case, the entire string will be processed before converting to HEX

4) Initialize the output value to 0.

5) Shift your output value to the left one bit and add the 0 or 1 from step 2

6) Repeat till the string is used up. Using a long-long, you can get up to 64 bits. Make sure to test for overflow or limit the size of the string.

7) Convert your output value into Hex. Grab 4 bits at a time from the output value and shift these into the low order bits of a char. Don't bother with the array of HEX digits, it's too cumbersome. Try this instead. If the value of the char is less than 10, simply add '0'. If the value is 10 or greater, then add ('A'-10). How you pick off each 4 bits and shift them around is up to you.

See this post for a code example:
http://www.daniweb.com/software-development/c/threads/153524/951124#post951124

Many of your suggestion assume too much in the environment of the user. It's also limiting to 64 bits -- assuming that's even possible for the user. These modifications will allow you to deal with any size binary value you choose -- based on the input array:

1) Don't use the %d format in scanf. Read the data in as a character string,

Correct -- use fgets()

2) All characters in the input string will be either '0' or '1'. Use the characteristics of the character values to your advantage. Subtract '0' from each character and you will be left with int 0 or 1.

Yes. Start at the beginning and subtract until you get to a non-1/0. This will, of course, be the end of your binary string, and your conversion start point.

3) You have a choice to make here. You can start at the end of the input string (the LSB) and work backwards, or at the beginning of the string (MSB) and work forwards. Each has it's advantages. Starting with the MSB is simpler logically. I'll illustrate this here In this case, the entire string will be processed before converting to HEX

Start at the end.

4) Initialize the output value to 0.

Actually, initialize the output value to SPACEs. You are going to load your HEX values into a string. Since you know how long the binary value is, you know how long the HEX value will be. Start an index
1) at the end of the HEX string. When done you will have a SPACE-filled HEX value.
2) into the HEX string so the 1st value is in HEX[0]. You will have exactly the number of HEX values in the array.
In either case, set HEX[yourIndex+1] to '\0' to end the string.

5) Shift your output value to the left one bit and add the 0 or 1 from step 2
6) Repeat till the string is used up. Using a long-long, you can get up to 64 bits. Make sure to test for overflow or limit the size of the string.

What makes you think long-long is available to the user? Personally, I doubt it. And this technique allows you to deal with any size binary the OP wants to allow -- based solely on the size of the input character array:

Set HexTemp to 0
Take the last 4 values and make a hex value from them. There are many ways to do it, bit-shift, multiply by 2 in a loop, however you want.

Now your HexTemp has the hex value. Use HexTemp as an index into the hexadecimal[16] array and load that value into the HEX array.

Repeat with the next 4 values, continuing until done.

7) Convert your output value into Hex. Grab 4 bits at a time from the output value and shift these into the low order bits of a char. Don't bother with the array of HEX digits, it's too cumbersome. Try this instead. If the value of the char is less than 10, simply add '0'. If the value is 10 or greater, then add ('A'-10). How you pick off each 4 bits and shift them around is up to you.

Completely disagree. There is nothing cumbersome about using the value as an index

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.