4 Replies - 6551 Views - Last Post: 23 February 2013 - 08:45 PM Rate Topic: -----

#1 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Using a loop to convert hex to binary?

Posted 18 February 2013 - 11:48 PM

So this code changes hexadecimal to binary digits. A specific hexadecimal number I should add. It is not built for all.

#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
	int decimal, y, z, x, w, dec, dec1, dec2, a, b, c, d, e, v, u, t, s, r, q, p, o, dec3, dec4, dec5, dec6, dec7, dec8, dec9, dec10;
	printf("The Hexadecimal number is: 0xCCCDB\n");
	a = 12 * pow(16.0, 4); //C \ 
	b = 12 * pow(16.0, 3); //C - Hex value of 'C' is 12, so we take 12 times 16^(position of C in the hex code)
	c = 12 * pow(16.0, 2); //C /
	d = 13 * pow(16.0, 1); //D - Hex value of 'D' is 13, so we take 13 times 16^(position of D in hex code)
	e = 11 * pow(16.0, 0); //B - Hex value of 'B' is 11, so we take 11 times 16^(position of B in hex code)

	decimal = a + b + c + d + e;
	printf("Decimal = %d\n", decimal);
		//Calculates 4 bit binary digit for the hexadecimal digit 'B'
		z = decimal % 2;
		dec = decimal / 2;
		y = dec % 2;
		dec1 = dec / 2;
		x = dec1 % 2;
		dec2 = dec1 / 2;
		w = dec2 % 2;
		dec3 = dec2 / 2;
		//Calculates 4 bit binary digit for the hexadecimal digit 'D'
		v = dec3 % 2;
		dec4 = dec3 / 2;
		u = dec4 % 2;
		dec5 = dec4 / 2;
		t = dec5 % 2;
		dec6 = dec5 / 2;
		s = dec6 % 2;
		dec7 = dec6 / 2;
		//Calculates 4 bit binary digit for the hexadecimal digit 'C'
		r = dec7 % 2;
		dec8 = dec7 / 2;
		q = dec8 % 2;
		dec9 = dec8 / 2;
		p = dec9 % 2;
		dec10 = dec9 / 2;
		o = dec10 % 2;
		printf("%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d%d %d%d%d%d", o, p, q, r, o, p, q, r, o, p, q, r, s, t, u, v, w, x, y, z); //Order variables backwards to print correct order of binary number.

	getch();
	return 0;
}


I mean, this does exactly what I want... But I was just wondering if there is a simpler way to do this with a loop? I tried a for() and a while() loop but just failed at it. They turned into infinity loops and I couldn't break them hahaha so I went with plan B above.

Just wondering for future reference.

Is This A Good Question/Topic? 0
  • +

Replies To: Using a loop to convert hex to binary?

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Using a loop to convert hex to binary?

Posted 19 February 2013 - 03:42 AM

Well, I would like to ask you one question...Why have you used 16.0 in your calculation when you have declared your variables as int.

Nevertheless, here is my take on the conversion using just 1 while loop and 16 switch statements.

#define MYSIZE 100

int main()
{
    char binaryNum[MYSIZE],hexaDec[MYSIZE];
     int i=0;

    printf("Enter hexadecimal number: ");
    scanf("%s",hexaDec);
    
    printf("\The binary value of the hexadecimal number is: ");
      while(hexaDec[i])
     {
        switch(hexaDec[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': printf("1010"); break;
             case 'B': printf("1011"); break;
             case 'C': printf("1100"); break;
             case 'D': printf("1101"); break;
             case 'E': printf("1110"); break;
             case 'F': printf("1111"); break;
             default:  printf("\nInvalid hexadecimal digit %c ,hexaDec[i]);
             return 0;
         }
         i++;
     }
    return 0;
}


Edit : Corrected a silly typo...

regards,
Raghav

This post has been edited by raghav.naganathan: 19 February 2013 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Using a loop to convert hex to binary?

Posted 19 February 2013 - 04:32 AM

Here's a version that uses a similar method to your first post:

#include <stdio.h>

void toBin(int v); 
int main()
{
  char string[] = "0xCCCDB";
  int val;
  //convert string to an int
  sscanf(string, "%x", &val);
  toBin(val);

  return 0;
}

void toBin(int v)
{
  int arr[31];
  int i;
  for (i=0; i <31 && v != 0; i++)
  {
    arr[i] = v%2;
    v = v/2;
  }
  i--;
  while(i>=0)
  {
    printf("%d",arr[i]);
    i--;
  }
}


This post has been edited by simeesta: 19 February 2013 - 04:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Using a loop to convert hex to binary?

Posted 19 February 2013 - 06:40 PM

Quote

Nevertheless, here is my take on the conversion using just 1 while loop and 16 switch statements.

But 16 switch cases :( I think some bit operations can clean those up :)

#include <stdio.h>
#include <ctype.h>

#define ALPHA_TO_HEX(c) c - 'A' + 10
#define NUM_TO_HEX(c) c - '0'

void printNibble(unsigned char nib) {
   int mask = 0x01;
   int i=3;
   for(; i>=0; i--)
      printf("%d", (nib >> i) & mask);
}

int printBin(char *hex) {
   for(; *hex; hex++) {
      if(isdigit(*hex))
         printNibble(NUM_TO_HEX(*hex));
      else if(*hex >= 'A' && *hex <='F')
         printNibble(ALPHA_TO_HEX(*hex));
      else
         break;
   }
   printf("\n");
   return !*hex;
}

int main() {
   char hex[50];

   while(scanf("%49s", hex) != EOF) {
      if(!printBin(hex))
         fprintf(stderr, "Invalid Hex Input\n");
   }

   return 0;
}


This post has been edited by jjl: 19 February 2013 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Using a loop to convert hex to binary?

Posted 23 February 2013 - 08:45 PM

This is one way but may not be acceptable as it hard codes the hex number
#include<iostream>
#include<bitset>

int main()
{
    using namespace std;
    short c = 0xabc;
    cout<<"\nthe hex number to convert to binary format is "<<hex<<c;
    bitset<16> y(c);
    cout<<"\nin binary format this is ";
    cout<<y;


    return 0;
}

/*
the hex number to convert to binary format is abc
in binary format this is 0000101010111100
Process returned 0 (0x0) execution time : 0.063 s
Press any key to continue*/

This post has been edited by buffalobill: 23 February 2013 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1