Decimal to any-base is a C function wich convert positive decimal integer to any positive ascii base with the base's alphabet given in input and return it in a dynamically allocated string(recursive way)
More...
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
|
bool | isbad_alphabet (const char *alphabet) |
| for IO operations More...
|
|
uint64_t | converted_len (uint64_t nb, short base) |
| Calculate the final length of the converted number. More...
|
|
void | convertion (uint64_t nb, const char *alphabet, short base, char *converted) |
| Convert positive decimal integer into anybase recursively. More...
|
|
char * | decimal_to_anybase (uint64_t nb, const char *alphabet) |
| decimal_to_anybase ensure the validity of the parameters and convert any unsigned integers into any ascii positive base More...
|
|
static void | test () |
| Self-test implementations. More...
|
|
int | main () |
| Main function. More...
|
|
Decimal to any-base is a C function wich convert positive decimal integer to any positive ascii base with the base's alphabet given in input and return it in a dynamically allocated string(recursive way)
- Author
- jucollet972
◆ converted_len()
uint64_t converted_len |
( |
uint64_t |
nb, |
|
|
short |
base |
|
) |
| |
Calculate the final length of the converted number.
- Parameters
-
nb | to convert |
base | calculated from alphabet |
- Returns
- Converted nb string length
uint64_t converted_len(uint64_t nb, short base)
Calculate the final length of the converted number.
Definition: decimal_to_any_base.c:42
◆ convertion()
void convertion |
( |
uint64_t |
nb, |
|
|
const char * |
alphabet, |
|
|
short |
base, |
|
|
char * |
converted |
|
) |
| |
Convert positive decimal integer into anybase recursively.
- Parameters
-
nb | to convert |
alphabet | inputed by user used for base convertion |
base | calculated from alphabet |
converted | string filled with the convertion's result |
- Returns
- void
60 *(converted) = *(alphabet + nb%base);
62 convertion(nb/base, alphabet, base, --converted);
void convertion(uint64_t nb, const char *alphabet, short base, char *converted)
Convert positive decimal integer into anybase recursively.
Definition: decimal_to_any_base.c:58
◆ decimal_to_anybase()
char * decimal_to_anybase |
( |
uint64_t |
nb, |
|
|
const char * |
alphabet |
|
) |
| |
decimal_to_anybase ensure the validity of the parameters and convert any unsigned integers into any ascii positive base
- Parameters
-
nb | to convert |
base's | alphabet |
- Returns
- nb converted on success
-
NULL on error
81 uint64_t base = strlen(alphabet);
83 converted =
malloc(
sizeof(
char) * (final_len + 1));
84 converted[final_len] = 0;
85 convertion(nb, alphabet, base, converted + final_len - 1);
bool isbad_alphabet(const char *alphabet)
for IO operations
Definition: decimal_to_any_base.c:20
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
◆ isbad_alphabet()
bool isbad_alphabet |
( |
const char * |
alphabet | ) |
|
for IO operations
for strchr and strlen for CPU arch's optimized int types for boolean types for assert for malloc and free
Checking if alphabet is valid
- Parameters
-
base | alphabet inputed by user |
- Returns
- int64_t as success or not
21 uint64_t len = strlen(alphabet);
28 for (
int i = 0; i < len ; i++) {
30 if (strchr(alphabet + i + 1, alphabet[i]))
◆ main()
Main function.
- Returns
- 0 on exit
static void test()
Self-test implementations.
Definition: decimal_to_any_base.c:94
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Self-test implementations.
- Returns
- void
97 char* reference = NULL;
102 for (
int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
103 assert(ret[i] == reference[i]);
110 reference =
"18446744073709551615";
112 for (
int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
113 assert(ret[i] == reference[i]);
120 reference =
"18446744073709551615";
122 for (
int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
123 assert(ret[i] == reference[i]);
130 reference =
"101010";
132 for (
int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
133 assert(ret[i] == reference[i]);
142 for (
int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
143 assert(ret[i] == reference[i]);
152 for (
int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
153 assert(ret[i] == reference[i]);
158 printf(
"[+] All tests have successfully passed!\n");
char * decimal_to_anybase(uint64_t nb, const char *alphabet)
decimal_to_anybase ensure the validity of the parameters and convert any unsigned integers into any a...
Definition: decimal_to_any_base.c:73
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26