Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
decimal_to_any_base.c File Reference

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>
Include dependency graph for decimal_to_any_base.c:

Functions

bool isbad_alphabet (const char *alphabet)
 for IO operations
 
uint64_t converted_len (uint64_t nb, short base)
 Calculate the final length of the converted number.
 
void convertion (uint64_t nb, const char *alphabet, short base, char *converted)
 Convert positive decimal integer into anybase recursively.
 
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
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

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

Function Documentation

◆ converted_len()

uint64_t converted_len ( uint64_t  nb,
short  base 
)

Calculate the final length of the converted number.

Parameters
nbto convert
basecalculated from alphabet
Returns
Converted nb string length
42 {
43 /* Counting the number of characters translated to the base*/
44 if (nb > base - 1) {
45 return (converted_len(nb/base, base) + 1);
46 }
47 return 1;
48}
uint64_t converted_len(uint64_t nb, short base)
Calculate the final length of the converted number.
Definition decimal_to_any_base.c:42
Here is the call graph for this function:

◆ convertion()

void convertion ( uint64_t  nb,
const char *  alphabet,
short  base,
char *  converted 
)

Convert positive decimal integer into anybase recursively.

Parameters
nbto convert
alphabetinputed by user used for base convertion
basecalculated from alphabet
convertedstring filled with the convertion's result
Returns
void
58 {
59 /* Recursive convertion */
60 *(converted) = *(alphabet + nb%base);
61 if (nb > base - 1) {
62 convertion(nb/base, alphabet, base, --converted);
63 }
64}
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
Here is the call graph for this function:

◆ 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
nbto convert
base'salphabet
Returns
nb converted on success
NULL on error
73 {
74 char* converted;
75
76 /* Verify that alphabet is valid */
77 if (isbad_alphabet(alphabet)) {
78 return NULL;
79 }
80 /* Convertion */
81 uint64_t base = strlen(alphabet);
82 uint64_t final_len = converted_len(nb, base);
83 converted = malloc(sizeof(char) * (final_len + 1));
84 converted[final_len] = 0;
85 convertion(nb, alphabet, base, converted + final_len - 1);
86 return converted;
87}
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
Here is the call graph for this function:

◆ 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
basealphabet inputed by user
Returns
int64_t as success or not
20 {
21 uint64_t len = strlen(alphabet);
22
23 /* Checking th lenght */
24 if (len < 2) {
25 return true;
26 }
27 /* Browse the alphabet */
28 for (int i = 0; i < len ; i++) {
29 /* Searching for duplicates */
30 if (strchr(alphabet + i + 1, alphabet[i]))
31 return true;
32 }
33 return false;
34}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
166{
167 test(); // run self-test implementations
168 return 0;
169}
static void test()
Self-test implementations.
Definition decimal_to_any_base.c:94
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
95{
96 char* ret = NULL;
97 char* reference = NULL;
98
99 /* min dec*/
100 reference = "0";
101 ret = decimal_to_anybase(0, "0123456789");
102 for (int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
103 assert(ret[i] == reference[i]);
104 }
105 if (ret != NULL) {
106 free(ret);
107 }
108
109 /* max dec*/
110 reference = "18446744073709551615";
111 ret = decimal_to_anybase(18446744073709551615, "0123456789");
112 for (int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
113 assert(ret[i] == reference[i]);
114 }
115 if (ret != NULL) {
116 free(ret);
117 }
118
119 /* negative dec*/
120 reference = "18446744073709551615";
121 ret = decimal_to_anybase(-1, "0123456789");
122 for (int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
123 assert(ret[i] == reference[i]);
124 }
125 if (ret != NULL) {
126 free(ret);
127 }
128
129 /* bin */
130 reference = "101010";
131 ret = decimal_to_anybase(42, "01");
132 for (int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
133 assert(ret[i] == reference[i]);
134 }
135 if (ret != NULL) {
136 free(ret);
137 }
138
139 /* octal */
140 reference = "52";
141 ret = decimal_to_anybase(42, "01234567");
142 for (int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
143 assert(ret[i] == reference[i]);
144 }
145 if (ret != NULL) {
146 free(ret);
147 }
148
149 /* hexa */
150 reference = "2A";
151 ret = decimal_to_anybase(42, "0123456789ABCDEF");
152 for (int i = 0; i < strlen(reference) && i < strlen(ret); i++) {
153 assert(ret[i] == reference[i]);
154 }
155 if (ret != NULL) {
156 free(ret);
157 }
158 printf("[+] All tests have successfully passed!\n");
159}
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
Here is the call graph for this function: