How can I convert integers to binary or hexadecimal?
Answers were Sorted based on User's Feedback
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 |
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 |
Why does this code crash?
If errno contains a nonzero number, is there an error?
How a string is stored in c?
using for loop sum 2 number of any 4 digit number in c language
Can we replace the struct function in tree syntax with a union?
how to capitalise first letter of each word in a given string?
What is the use of define in c?
Print all the palindrome numbers.If a number is not palindrome make it one by attaching the reverse to it. eg:123 output:123321 (or) 12321
question-how to run a c programme.
Is a pointer a kind of array?
what is the stackpointer
WHAT IS THE DIFFERENCE BETWEEN malloc() and calloc() in c file management?
28 Answers 3D PLM, Code Studio, Deltech, IBM,