From bfce8f0d0d828209ec0bec71371ee94a7ad62d3e Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Tue, 4 Jul 2023 17:03:53 -0500 Subject: Initial commit --- three/conv.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 three/conv.c (limited to 'three/conv.c') diff --git a/three/conv.c b/three/conv.c new file mode 100644 index 0000000..d3e654e --- /dev/null +++ b/three/conv.c @@ -0,0 +1,65 @@ +#include +#include + +void +reverse(char s[]) +{ + int i = 0; + while (s[i] != '\0') { + ++i; + } + --i; + char c; + for (int j = 0; j < i / 2; ++j) { + c = s[i - j]; + s[i - j] = s[j]; + s[j] = c; + } +} + +void +itoa(int n, char s[]) +{ + int i, sign; + if ((sign = n) < 0) { + n = -n - 1; + } + i = 0; + do { + s[i++] = n % 10 + '0'; + } while ((n /= 10) > 0); + if (sign < 0) { + s[i++] = '-'; + } + s[i] = '\0'; + reverse(s); +} + +void +itob(int n, char s[], int b) +{ + int i, sign; + if ((sign = n) < 0) { + n = -n; + } + i = 0; + do { + s[i++] = (n % b <= 9) ? n % b + '0' : n % b - 10 + 'a'; + } while ((n /= b) > 0); + if (sign < 0) { + s[i++] = '-'; + } + s[i] = '\0'; + reverse(s); +} + +int +main() +{ + char s[32]; + itoa(INT_MIN, s); + printf("%s\n", s); + itob(380, s, 16); + printf("%s\n", s); + return 0; +} -- cgit v1.2.3