Jump to content

Hexa convert to binary. sum all binary numbers.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
iera_yahaya

iera_yahaya

    Newbie

  • Members
  • Pip
  • 3 posts
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...

#include<stdio.h>

#include<string.h>


int main() 

{ 

int i,sum=0; 

char s[20];           

     printf("Convertion from Hexadecimal to Binary\n");          

     printf("Enter a Hexadecimal number of array: \n");     

     

     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;

}  





#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
You take the user input as text. Take the text that represents hexadecimal values and convert it into an int. Then add the ints to get the sum. And then print the sum in binary.

You could use your switch statement for the "hex conversion", something like:
int value;
switch(s[i])           
 {             
case '0':             
 printf("0000");              
 value = 0;
break;            
 case '1':              
printf("0001");              
 value = 0;
break;            
/* etc. */
}
Outputting an integer as its binary representation in text is a fun exercise -- there are many ways to do it. I've got my favorites, but take a shot at it first.

#3
iera_yahaya

iera_yahaya

    Newbie

  • Members
  • Pip
  • 3 posts

Quote

Thank you... i've done the code and it runs smoothly like what i want.

#include <stdio.h>



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("\n%d\n",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("\n");

}




#4
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
I answered you other thread.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#5
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

dcs said:

You take the user input as text. Take the text that represents hexadecimal values and convert it into an int. Then add the ints to get the sum. And then print the sum in binary.

You could use your switch statement for the "hex conversion", something like:
int value;

switch(s[i])           

 {             

case '0':             

 printf("0000");              

 value = 0;

break;            

 case '1':              

printf("0001");              

 value = 0;

break;            

/* etc. */

}
Outputting an integer as its binary representation in text is a fun exercise -- there are many ways to do it. I've got my favorites, but take a shot at it first.

Why not using a function that converts from hex to binary? More efficient and easier to write. Check out this guy's other thread related on the same thing.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

Davide said:

Why not using a function that converts from hex to binary? More efficient and easier to write. Check out this guy's other thread related on the same thing.
In part because I'd use the standard library function strtol to convert the text to an int (much easier than writing my own, and probably better too), and then use one of the bit-output functions I've previously written.

But this looked to be an exercise (so I'd assum no strtol). And I was curious which approach would be chosen for the bit output.