Created
August 12, 2012 02:18
-
-
Save khakimov/3329057 to your computer and use it in GitHub Desktop.
decimal to hexadecimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Exercise 3-4 K&R. | |
To convert a decimal number x to hexadecimal, we can repeatedly divide x by 16, | |
giving a quotient q and a remainder r, such that x = q * 16 + r. | |
*/ | |
#include <stdio.h> | |
void itoh(int n); | |
int convert(int s); | |
int main() | |
{ | |
itoh(314156); | |
return 0; | |
} | |
void itoh(int n) | |
{ | |
int i = 0, j =0; | |
char buf[100]; | |
while(n != 0) | |
{ | |
i = n % 16; | |
if (i > 0) | |
{ | |
buf[j++] = convert(i); | |
n = (n - i) / 16; | |
} | |
} | |
buf[j] = '\0'; | |
printf("%s\n", buf); | |
} | |
int convert(int s) | |
{ | |
int c; | |
char hex[] = {'A', 'B', 'C', 'D', 'E', 'F'}; | |
c = (s > 0 && s < 9) ? s + '0' : hex[s - 10]; | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if i = 16 or i = 32 ?