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>
|
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. More...
|
|
Convert a positive integer to string (non-standard function) representation.
◆ 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
-
value | Value to be converted to a string. |
dest | pointer to array in memory to store the resulting null-terminated string. |
base | Numerical 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.
27 const char hex_table[] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
28 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
33 dest[len++] = hex_table[value % base];
38 for (
int i = 0, limit = len / 2; i < limit; ++i)
41 dest[i] = dest[len - 1 - i];
42 dest[len - 1 - i] = t;
◆ main()
Driver Code.
static void test()
Test function.
Definition: int_to_string.c:51
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Test function.
- Returns
void
57 for (
int i = 1; i <= 100; ++i)
60 int value = rand() % 100;
64 snprintf(str1,
MAX_SIZE,
"%o", value);
66 snprintf(str1,
MAX_SIZE,
"%d", value);
68 snprintf(str1,
MAX_SIZE,
"%x", value);
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
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition: malloc_dbg.h:22
const unsigned long long MAX_SIZE
for assert for standard input output for general purpose standard library
Definition: prime_seive.c:11