Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
int_to_string.c File Reference

Convert a positive integer to string (non-standard function) representation. More...

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for int_to_string.c:

Functions

char * int_to_string (uint16_t value, char *dest, int base)
 Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter. More...
 
static void test ()
 Test function. More...
 
int main ()
 Driver Code.
 

Detailed Description

Convert a positive integer to string (non-standard function) representation.

Function Documentation

◆ int_to_string()

char* int_to_string ( uint16_t  value,
char *  dest,
int  base 
)

Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.

Parameters
valueValue to be converted to a string.
destpointer to array in memory to store the resulting null-terminated string.
baseNumerical base used to represent the value as a string, between 2 and 16, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
Returns
A pointer to the resulting null-terminated string, same as parameter str.
Note
The destination array must be pre-allocated by the calling function.
26 {
27  const char hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7',
28  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
29 
30  int len = 0;
31  do
32  {
33  dest[len++] = hex_table[value % base];
34  value /= base;
35  } while (value != 0);
36 
37  /* reverse characters */
38  for (int i = 0, limit = len / 2; i < limit; ++i)
39  {
40  char t = dest[i];
41  dest[i] = dest[len - 1 - i];
42  dest[len - 1 - i] = t;
43  }
44  dest[len] = '\0';
45  return dest;
46 }

◆ test()

static void test ( void  )
static

Test function.

Returns
void
52 {
53  const int MAX_SIZE = 100;
54  char *str1 = (char *)calloc(sizeof(char), MAX_SIZE);
55  char *str2 = (char *)calloc(sizeof(char), MAX_SIZE);
56 
57  for (int i = 1; i <= 100; ++i) /* test 100 random numbers */
58  {
59  /* Generate value from 0 to 100 */
60  int value = rand() % 100;
61 
62  // assert(strcmp(itoa(value, str1, 2), int_to_string(value, str2, 2)) ==
63  // 0);
64  snprintf(str1, MAX_SIZE, "%o", value); //* standard C - to octal */
65  assert(strcmp(str1, int_to_string(value, str2, 8)) == 0);
66  snprintf(str1, MAX_SIZE, "%d", value); /* standard C - to decimal */
67  assert(strcmp(str1, int_to_string(value, str2, 10)) == 0);
68  snprintf(str1, MAX_SIZE, "%x", value); /* standard C - to hexadecimal */
69  assert(strcmp(str1, int_to_string(value, str2, 16)) == 0);
70  }
71 
72  free(str1);
73  free(str2);
74 }
Here is the call graph for this function:
MAX_SIZE
#define MAX_SIZE
maximum number of elements in the set
Definition: union_find.c:8
int_to_string
char * int_to_string(uint16_t value, char *dest, int base)
Converts an integer value to a null-terminated string using the specified base and stores the result ...
Definition: int_to_string.c:25