How can I convert integers to binary or hexadecimal?
Answer Posted / 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 View All Answers
If you know then define #pragma?
What is ponter?
Can you define which header file to include at compile time?
p*=(++q)++*--p when p=q=1 while(q<=6)
a construct the"else" part of "if" statement contains anoth "if else" statement is called a) if-else b) else-if-else c) if-else-if-else d) chain if/if-else-if
Why is this loop always executing once?
What are the features of c language?
What are different types of operators?
When should a far pointer be used?
What is structure and union in c?
What is define c?
How can you allocate arrays or structures bigger than 64K?
while initialization of array why we use a[][2] why not a[2][]...?
Explain how can type-insensitive macros be created?
What is use of integral promotions in c?