How can I convert integers to binary or hexadecimal?

Answers were Sorted based on User's Feedback



How can I convert integers to binary or hexadecimal?..

Answer / sabarish

for decimal to hexa its very simple.

void main()
{
int a=10;

printf("%x",a); // returns the hexa decimal equivalent
}

Is This Answer Correct ?    14 Yes 2 No

How can I convert integers to binary or hexadecimal?..

Answer / sabarish

code to convert binary to decimal

void dec2bin(long decimal, char *binary)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
int old_decimal; // for test
char temp[80];

// take care of negative input
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
old_decimal = decimal; // for test
remain = decimal % 2;
// whittle down the decimal number
decimal = decimal / 2;
// this is a test to show the action
printf("%d/2 = %d remainder = %d\n", old_decimal,
decimal, remain);
// converts digit 0 or 1 to character '0' or '1'
temp[k++] = remain + '0';
} while (decimal > 0);

if (neg_flag)
temp[k++] = '-'; // add - sign
else
temp[k++] = ' '; // space

// reverse the spelling
while (k >= 0)
binary[n++] = temp[--k];

binary[n-1] = 0; // end with NULL
}

Is This Answer Correct ?    4 Yes 4 No

Post New Answer

More C Interview Questions

simple program of graphics and their output display

0 Answers   Elysium,


main() { clrscr(); } clrscr();

6 Answers   ME, Wipro,


What is the incorrect operator form following list(== , <> , >= , <=) and what is the reason for the answer?

0 Answers  


What is the use of header files?

0 Answers  


Code for calculating square root without using library function, of math.h

4 Answers   IBM,






What is cohesion and coupling in c?

0 Answers  


What is the value of h?

0 Answers  


What are reserved words?

0 Answers  


how to get the starting address of file stored in harddisk through 'C'program.

2 Answers   Siemens,


How do you search data in a data file using random access method?

0 Answers  


FIND THE OUTPUT IF THE INPUT IS 5 5.75 void main() { int i=1; float f=2.25; scanf("%d%f",&i,&f); printf("%d %f",,i,f); } ANSWER IS 5 AND 2.25 WHY?

4 Answers   Wipro,


What are identifiers c?

0 Answers  


Categories