libft
Loading...
Searching...
No Matches
Files | Functions
Number Conversion Utilities

String ↔ number conversion and formatting. More...

Files

file  ft_convert.h
 Numeric conversion utility functions.
 
file  ft_number_conversion.c
 Number-to-string and string-to-number conversion functions.
 

Functions

long long ft_atoi (const char *str)
 Converts a string to a signed integer.
 
long long ft_atoll (const char *str)
 Converts a string to a long long value.
 
static size_t ft_baselen (unsigned long long n, int base)
 Calculates the number of digits to represent a value in a given base.
 
static bool ft_check_overflow (long long num, char digit, int sign)
 Checks for overflow when converting a string to a long long integer.
 
char * ft_itoa (int n)
 Converts an integer to a string.
 
char * ft_itoa_base (unsigned long long n, int base, int uppercase)
 Converts an unsigned integer to a string in a given base.
 
char * ft_utoa (unsigned long long n)
 Converts an unsigned long long to a decimal string.
 

Detailed Description

String ↔ number conversion and formatting.

This group provides functions to convert strings to numbers (like ft_atoi and ft_atoll) with overflow detection, and to format unsigned integers into strings, optionally in base 2 to 16.

This group includes:

Function Documentation

◆ ft_atoi()

long long ft_atoi ( const char *  str)

#include <include/ft_convert.h>

Converts a string to a signed integer.

Converts the null-terminated string str to a long long value. Handles optional leading whitespace, sign, and overflow.

Parameters
strThe string to convert.
Returns
The converted value or ATOI_ERROR on overflow.
See also
ft_check_overflow
ATOI_ERROR

◆ ft_atoll()

long long ft_atoll ( const char *  str)

#include <include/ft_convert.h>

Converts a string to a long long value.

Similar to ft_atoi, but does not cast to int and returns a full long long result. Handles whitespace, signs, and overflow.

Parameters
strThe string to convert.
Returns
The converted value or ATOI_ERROR on overflow.
See also
ft_check_overflow
ATOI_ERROR

◆ ft_baselen()

static size_t ft_baselen ( unsigned long long  n,
int  base 
)
static

#include <srcs/convert/ft_number_conversion.c>

Calculates the number of digits to represent a value in a given base.

Computes how many characters are needed to express n in base base. Used internally by ft_itoa_base.

Parameters
nThe number to analyze.
baseThe numerical base (2–16).
Returns
The number of digits required to represent n.

◆ ft_check_overflow()

static bool ft_check_overflow ( long long  num,
char  digit,
int  sign 
)
static

#include <srcs/convert/ft_number_conversion.c>

Checks for overflow when converting a string to a long long integer.

Evaluates whether appending a digit to num would cause it to overflow a signed 32-bit integer, taking into account the current sign.

Parameters
numThe current number being built.
digitThe current character digit to append.
signThe number sign (1 for positive, -1 for negative).
Returns
true if overflow would occur, false otherwise.
See also
ft_atoi

◆ ft_itoa()

char * ft_itoa ( int  n)

#include <include/ft_convert.h>

Converts an integer to a string.

Allocates and returns a new string representing the integer n. Handles negative numbers and returns NULL if allocation fails.

Parameters
nThe integer to convert.
Returns
A null-terminated string representing n, or NULL on failure.

◆ ft_itoa_base()

char * ft_itoa_base ( unsigned long long  n,
int  base,
int  uppercase 
)

#include <include/ft_convert.h>

Converts an unsigned integer to a string in a given base.

Converts the unsigned number n to a string in base base (2–16). Uses lowercase or uppercase digits based on uppercase.

Parameters
nThe number to convert.
baseThe base for conversion (2–16).
uppercaseIf 1, use uppercase letters for bases > 10.
Returns
A newly allocated null-terminated string, or NULL on failure.
See also
ft_baselen

◆ ft_utoa()

char * ft_utoa ( unsigned long long  n)

#include <include/ft_convert.h>

Converts an unsigned long long to a decimal string.

Converts the value n to its decimal string representation. A null-terminated string is returned. The caller must free it.

Parameters
nThe number to convert.
Returns
A dynamically allocated string, or NULL on allocation failure.