0

I'm trying to convert hexadecimal values to Binary.
I've done Decimal values to Binary up to 32 bits in this way:

#include <stdio.h>
int showbits(int);/**************function*prototype******************************/

int main()
{
unsigned int num;

printf("enter the number.");
scanf("%d",&num);

printf("%d in binary is  ",num);
printf("\n");

showbits(num);/*********************function*call********************************/

return 0;
}

showbits (int n)/******************function*definition****************************/
																				//*
{
  int i,k,andmask;

  for(i=31;i>=0;i--)
  {
     andmask = 1<<i;
     k=n&andmask;
     k==0?printf("0"):printf("1");
  }
}

Now I wan't to add hexadecimal values in it.

2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by D33wakar
Featured Replies
  • 1
    Narue 5,707   5 Years Ago

    Note that scanf is converting the display representation to the value, so %d will look for decimal values. %x will allow you to read hexadecimal values. Read More

0

What makes you think there's a difference? Decimal vs. hexadecimal is a display representation, the underlying bits of corresponding values are unchanged.

0

What do you think the problem is?
I know

Decimal vs. hexadecimal is a display representation, the underlying bits of corresponding values are unchanged

In the above program, the user can only enter decimal digits 0-9.
I need a code that should be able to give :
00000000000000000000000000001111 for the digit F.

1

Note that scanf is converting the display representation to the value, so %d will look for decimal values. %x will allow you to read hexadecimal values.

0

Well,this is embarrassing.I'd never thought it that way.
I was looking for a data type that can hold alphanumeric digits.
But hey that's what discussion forums are for, right?
Thanks for the help Narue.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.