Hexa convert to binary. sum all binary numbers.

basically, i need to add 8 hexadecimal numbers. and each hexadecimal numbers need to be change to binary numbers. After that, i need to do summation for all binary numbers that i've changed.

for example = A+B+C+D+1+2+3+4
changed to binary = 1010+1011 +1100+1101+0001+0010+0011+0100
summation = 101110

i need to do this using C language...

[code]
#include
#include

int main()
{
int i,sum=0;
char s[20];
printf("Convertion from Hexadecimal to Binary
");
printf("Enter a Hexadecimal number of array:
");

for (i=0;i<8;i++)
scanf("%s",&s[i]);
printf("Binary Equivalent= ");
for (i=0;i<8;i++)
for(i=0;s[i]!=NULL;i++)

switch(s[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'a':
case 'A':
printf("1010");
break;
case 'b':
case 'B':
printf("1011");
break;
case 'c':
case 'C':
printf("1100");
break;
case 'd':
case 'D':
printf("1101");
break;
case 'e':
case 'E':
printf("1110");
break;
case 'f':
case 'F':
printf("1111");
break;
default:
printf("Entered number is not Hexadecimal.Printed value is not correct.");
break;
}

return 0;
}


[/code]

Comments

  • Hi,

    U pls refer the below code which i hav written and test with Visual studio2005 to convet Hex to bin and display it. After converting to bin and displaing it, what actually u wanted to do? pls let me know.


    #include
    #include

    main()
    {

    char array[100];
    int index, no_of_input;
    printf("Enter no of input:
    ");
    scanf("%d",&no_of_input); /*U configure no of elements according to ur need*/
    /*Get the inputs*/
    printf("Hex input:
    ");
    for(index = 0; index< no_of_input; index++)
    {
    scanf("%x", &array[index]);
    }
    printf("Binary output:
    ");
    /*Pass the char array and no of elements in the array to display the data in binary format*/
    hextobin(array, no_of_input);
    getch();

    }

    /*Function to convert hex to binary*/
    hextobin(char *array, int no_of_input)
    {
    int b, n, i = 8;

    while(no_of_input)
    {
    b = 8;
    for(n = 0; n < 4 ; n++)
    {
    if(( b & *array ) != 0)
    {
    printf("1");
    }
    else
    {
    printf("0");
    }
    b = b >> 1;
    }
    array++;
    no_of_input--;
    printf("
    ");
    }

    }
  • Thank you for helping me... i've got the code and already test it... Thank you.. But somehow, what i need is to add all binary numbers after changed it from hexademal. Something like C+5 = 1100+101 = 10001.
  • Oh! fine... if require the remaining part.let me know and i ll try to giv the soln...

    Bye..
  • Oh! fine... if require the remaining part.let me know and i ll try to giv the soln...

    Bye..
  • Oh! fine... if require the remaining part.let me know and i ll try to giv the soln...

    Bye..
  • hye.. actually, how to add binary numbers?
  • According to bin addition 1 + 1 = 10, 1 + 0 = 1 and 0 + 0 = 0


    So lets take ur example,

    C+5 = 1100+101 = 10001

    Here,

    1100
    0101
    ----
    10001
    -----

    Do the calculation from LSB

    1) 0 + 1 = 1
    2) 0 + 0 = 0
    3) 1 + 1 = 10 so 0 we have to write and 1 will be the carry
    4) 1 + 0 + 1(carry) = 10

    So ur ans is -> 10001

    My Suggn is, dont try to get the code from google unless u hav no idea on the concept. Because it will become a habit and for each and every thing u ll try to get the code from google...

    Try urself as much possible, even after, if ur not able to get the ans then go and surf in google and find the ans..




  • According to bin addition 1 + 1 = 10, 1 + 0 = 1 and 0 + 0 = 0


    So lets take ur example,

    C+5 = 1100+101 = 10001

    Here,

    1100
    0101
    ----
    10001
    -----

    Do the calculation from LSB

    1) 0 + 1 = 1
    2) 0 + 0 = 0
    3) 1 + 1 = 10 so 0 we have to write and 1 will be the carry
    4) 1 + 0 + 1(carry) = 10

    So ur ans is -> 10001

    My Suggn is, dont try to get the code from google unless u hav no idea on the concept. Because it will become a habit and for each and every thing u ll try to get the code from google...

    Try urself as much possible, even after ur not able to get the ans then surf in google..




  • Thank you for helping me.. i've try to do the code and i've got the result like what i want...


    the output that i want is for example i insert 5c6da5 and all those hexa numbers will be converted to binary such as 0101+1100+0110+1101+1010+0101 and it will come out with decimal numbers of total with 51. then that 51 will be converted back to binary which is 110011.

    5c6da5 = 0101+1100+0110+1101+1010+0101 = 51 = 110011.


    [code]
    #include


    void dec_bin(int number);

    void main()
    {
    char s [80];
    int i,value,sum=0;



    printf ("Type a string less than 80 characters:");
    gets(s);
    printf ("The string types is:");
    puts(s);

    printf("Binary Equivalent= ");

    for(i=0;s[i]!=NULL;i++)
    {
    switch(s[i])
    {
    case '0':
    printf("0000");
    value = 0;
    break;
    case '1':
    printf("0001");
    value = 1;
    break;
    case '2':
    printf("0010");
    value = 2;
    break;
    case '3':
    printf("0011");
    value = 3;
    break;
    case '4':
    printf("0100");
    value = 4;
    break;
    case '5':
    printf("0101");
    value = 5;
    break;
    case '6':
    printf("0110");
    value = 6;
    break;
    case '7':
    printf("0111");
    value = 7;
    break;
    case '8':
    printf("1000");
    value = 8;
    break;
    case '9':
    printf("1001");
    value = 9;
    break;
    case 'a':
    case 'A':
    printf("1010");
    value = 10;
    break;
    case 'b':
    case 'B':
    printf("1011");
    value = 11;
    break;
    case 'c':
    case 'C':
    printf("1100");
    value = 12;
    break;
    case 'd':
    case 'D':
    printf("1101");
    value = 13;
    break;
    case 'e':
    case 'E':
    printf("1110");
    value = 14;
    break;
    case 'f':
    case 'F':
    printf("1111");
    value = 15;
    break;
    default:
    printf("Entered number is not Hexadecimal.Printed value is not correct.");
    break;
    }
    sum=sum+value;
    printf("+");
    }

    printf("
    %d
    ",sum);

    (sum >= 0) && (sum < 256) ? dec_bin(sum) : exit(1);

    }


    void dec_bin(int number) {
    int x, y;
    x = y = 0;

    for(y = 7; y >= 0; y--) {
    x = number / (1 << y);
    number = number - x * (1 << y);
    printf("%d", x);
    }

    printf("
    ");
    }



    [/code]
  • It is fine, if u hav acheived d result wat u wanted..
    Keep exploring....
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories