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

what is dangling pointer?

1 Answers   LG Soft,


Why static is used in c?

0 Answers  


Write a C program on Centralized OLTP, Decentralized OLTP using locking mechanism, Semaphore using locking mechanism, Shared memory, message queues, channel of communication, sockets and a simple program on Saving bank application program using OLTP in IPC?

0 Answers  


What are the two types of structure?

0 Answers  


What is the meaning of && in c?

0 Answers  


a c code by using memory allocation for add ,multiply of sprase matrixes

0 Answers  


In C language what is a 'dangling pointer'?

0 Answers   Accenture,


Explain main function in c?

0 Answers  


Explain argument and its types.

0 Answers  


Dear Sir, we are required the bubble sorting programs Regs Prem

1 Answers  


Can the curly brackets { } be used to enclose a single line of code?

0 Answers  


In how much time you will write this c program? Prime nos from 1 to 1000

2 Answers   TCS,


Categories