45 lines
882 B
C
Executable file
45 lines
882 B
C
Executable file
#include <strings.h>
|
|
|
|
void num_to_ascii(int n, char str[]) {
|
|
int i, sign;
|
|
if ((sign = n) < 0) n = -n;
|
|
i = 0;
|
|
do {
|
|
str[i++] = n % 10 + '0';
|
|
} while ((n /= 10) > 0);
|
|
|
|
if (sign < 0) str[i++] = '-';
|
|
str[i] = '\0';
|
|
|
|
/* TODO: implement "reverse" */
|
|
}
|
|
|
|
void num_to_bits(int n, char str[]) {
|
|
int i = 0;
|
|
for (int j = 31; j >= 0; j--) {
|
|
str[i++] = (n & (1 << j)) ? '1' : '0';
|
|
}
|
|
str[i] = '\0';
|
|
}
|
|
|
|
void num_to_hex(int n, char str[]) {
|
|
int i = 0;
|
|
for (int j = 28; j >= 0; j -= 4) {
|
|
int hex_digit = (n >> j) & 0xF;
|
|
if (hex_digit < 10) {
|
|
str[i++] = hex_digit + '0';
|
|
} else {
|
|
str[i++] = hex_digit - 10 + 'A';
|
|
}
|
|
}
|
|
str[i] = '\0';
|
|
}
|
|
|
|
int strlen(char* str) {
|
|
int len = 0;
|
|
while (1) {
|
|
if (str[len] == 0)
|
|
return len;
|
|
len++;
|
|
}
|
|
}
|