CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Numeric Conversion

Modules

 Internal conversion functions
 

Functions

bool strToInt32 (int32 *out, strref s, int base, bool strict)
 
bool strToUInt32 (uint32 *out, strref s, int base, bool strict)
 
bool strToInt64 (int64 *out, strref s, int base, bool strict)
 
bool strToUInt64 (uint64 *out, strref s, int base, bool strict)
 
bool strFromInt32 (strhandle out, int32 i, uint16 base)
 
bool strFromUInt32 (strhandle out, uint32 i, uint16 base)
 
bool strFromInt64 (strhandle out, int64 i, uint16 base)
 
bool strFromUInt64 (strhandle out, uint64 i, uint16 base)
 
bool strToFloat32 (float32 *out, strref s, bool strict)
 
bool strToFloat64 (float64 *out, strref s, bool strict)
 
bool strFromFloat32 (strhandle out, float32 f)
 
bool strFromFloat64 (strhandle out, float64 f)
 

Detailed Description

Functions for converting between strings and numeric values. Both integer and floating-point conversions are supported.

Integer parsing

Integer parsing supports bases 2-36. Unlike standard C library functions, a leading '0' does NOT imply octal (base 8) - base 10 is always the default unless explicitly specified or the string begins with "0x" for hexadecimal.

The 'strict' parameter controls whether the entire string must be numeric:

Floating-point conversion

Float-to-string conversion uses the Grisu2 algorithm for efficient and accurate representation. The output format is chosen automatically (plain, decimal, or scientific notation) based on the magnitude of the value.

Error handling

Conversion functions return false on error and set cxerr:

Function Documentation

◆ strFromFloat32()

bool strFromFloat32 ( strhandle  out,
float32  f 
)

Converts a 32-bit floating-point value to a string

Formats the float using the Grisu2 algorithm for efficient and accurate conversion. The output format (plain decimal, scientific notation) is chosen automatically based on the magnitude. Special values are rendered as "inf", "-inf", or "nan".

Parameters
outOutput string (existing content destroyed)
fFloat value to convert
Returns
true on success, false on error

Example:

string s = 0;
strFromFloat32(&s, 3.14f); // "3.14"
strFromFloat32(&s, 1.5e10f); // "1.5e+10"
strFromFloat32(&s, 0.0f); // "0"
void strDestroy(strhandle ps)
bool strFromFloat32(strhandle out, float32 f)

◆ strFromFloat64()

bool strFromFloat64 ( strhandle  out,
float64  f 
)

Converts a 64-bit floating-point value to a string

Like strFromFloat32(), but for double-precision values. Uses Grisu2 algorithm for accurate conversion with automatic format selection.

Parameters
outOutput string (existing content destroyed)
fDouble value to convert
Returns
true on success, false on error

Example:

string s = 0;
strFromFloat64(&s, 3.141592653589793); // "3.141592653589793"
strFromFloat64(&s, 2.5e-10); // "2.5e-10"
bool strFromFloat64(strhandle out, float64 f)

◆ strFromInt32()

bool strFromInt32 ( strhandle  out,
int32  i,
uint16  base 
)

Converts a 32-bit signed integer to a string

Formats the integer in the specified base. Negative values are prefixed with '-'. For bases > 10, lowercase letters are used (a-z).

Parameters
outOutput string (existing content destroyed)
iInteger value to convert
baseNumeric base (2-36), typically 10 or 16
Returns
true on success, false on error

Example:

string s = 0;
strFromInt32(&s, 123, 10); // "123"
strFromInt32(&s, -42, 10); // "-42"
strFromInt32(&s, 255, 16); // "ff"
bool strFromInt32(strhandle out, int32 i, uint16 base)

◆ strFromInt64()

bool strFromInt64 ( strhandle  out,
int64  i,
uint16  base 
)

Converts a 64-bit signed integer to a string

Like strFromInt32(), but for 64-bit values.

Parameters
outOutput string (existing content destroyed)
iInteger value to convert
baseNumeric base (2-36), typically 10 or 16
Returns
true on success, false on error

◆ strFromUInt32()

bool strFromUInt32 ( strhandle  out,
uint32  i,
uint16  base 
)

Converts a 32-bit unsigned integer to a string

Formats the integer in the specified base. For bases > 10, lowercase letters are used (a-z).

Parameters
outOutput string (existing content destroyed)
iInteger value to convert
baseNumeric base (2-36), typically 10 or 16
Returns
true on success, false on error

Example:

string s = 0;
strFromUInt32(&s, 255, 10); // "255"
strFromUInt32(&s, 255, 16); // "ff"
bool strFromUInt32(strhandle out, uint32 i, uint16 base)

◆ strFromUInt64()

bool strFromUInt64 ( strhandle  out,
uint64  i,
uint16  base 
)

Converts a 64-bit unsigned integer to a string

Like strFromUInt32(), but for 64-bit values.

Parameters
outOutput string (existing content destroyed)
iInteger value to convert
baseNumeric base (2-36), typically 10 or 16
Returns
true on success, false on error

◆ strToFloat32()

bool strToFloat32 ( float32 *  out,
strref  s,
bool  strict 
)

Converts a string to a 32-bit floating-point value

Parses a string as a single-precision float. Supports standard decimal notation, scientific notation (e.g., "1.23e-4"), and special values ("inf", "nan"). Leading whitespace is skipped.

Parameters
outPointer to receive the parsed value
sString to parse
strictIf true, entire string must be numeric; if false, trailing chars allowed
Returns
true on success, false on error (invalid format)

Example:

float32 value;
strToFloat32(&value, _S"3.14", true); // value = 3.14
strToFloat32(&value, _S"-1.5e2", true); // value = -150.0
strToFloat32(&value, _S"inf", true); // value = infinity
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
bool strToFloat32(float32 *out, strref s, bool strict)

◆ strToFloat64()

bool strToFloat64 ( float64 *  out,
strref  s,
bool  strict 
)

Converts a string to a 64-bit floating-point value

Like strToFloat32(), but parses as double-precision. Supports decimal notation, scientific notation, and special values.

Parameters
outPointer to receive the parsed value
sString to parse
strictIf true, entire string must be numeric; if false, trailing chars allowed
Returns
true on success, false on error (invalid format)

Example:

float64 value;
strToFloat64(&value, _S"3.141592653589793", true);
strToFloat64(&value, _S"2.5e-10", true);
bool strToFloat64(float64 *out, strref s, bool strict)

◆ strToInt32()

bool strToInt32 ( int32 *  out,
strref  s,
int  base,
bool  strict 
)

Converts a string to a 32-bit signed integer

Parses a string as a signed integer in the specified base. Leading whitespace is skipped. An optional '+' or '-' sign is recognized. Hexadecimal strings may begin with "0x" or "0X".

Unlike strtol(), a leading '0' does NOT imply octal - base 10 is the default.

Parameters
outPointer to receive the parsed value
sString to parse
baseNumeric base (2-36), or 0 for auto-detect (10 or 16)
strictIf true, entire string must be numeric; if false, trailing chars allowed
Returns
true on success, false on error (invalid format or out of range)

Example:

int32 value;
strToInt32(&value, _S"123", 10, true); // value = 123
strToInt32(&value, _S"-42", 10, true); // value = -42
strToInt32(&value, _S"0xFF", 0, true); // value = 255
strToInt32(&value, _S"123abc", 10, false); // value = 123, ok
strToInt32(&value, _S"123abc", 10, true); // false, strict mode
bool strToInt32(int32 *out, strref s, int base, bool strict)

◆ strToInt64()

bool strToInt64 ( int64 *  out,
strref  s,
int  base,
bool  strict 
)

Converts a string to a 64-bit signed integer

Like strToInt32(), but for 64-bit values.

Parameters
outPointer to receive the parsed value
sString to parse
baseNumeric base (2-36), or 0 for auto-detect (10 or 16)
strictIf true, entire string must be numeric; if false, trailing chars allowed
Returns
true on success, false on error (invalid format or out of range)

◆ strToUInt32()

bool strToUInt32 ( uint32 *  out,
strref  s,
int  base,
bool  strict 
)

Converts a string to a 32-bit unsigned integer

Like strToInt32(), but parses as an unsigned value. A leading '-' sign is allowed and results in two's complement representation of the negative value.

Parameters
outPointer to receive the parsed value
sString to parse
baseNumeric base (2-36), or 0 for auto-detect (10 or 16)
strictIf true, entire string must be numeric; if false, trailing chars allowed
Returns
true on success, false on error (invalid format or out of range)

Example:

uint32 value;
strToUInt32(&value, _S"255", 10, true); // value = 255
strToUInt32(&value, _S"0xFF", 0, true); // value = 255
bool strToUInt32(uint32 *out, strref s, int base, bool strict)

◆ strToUInt64()

bool strToUInt64 ( uint64 *  out,
strref  s,
int  base,
bool  strict 
)

Converts a string to a 64-bit unsigned integer

Like strToUInt32(), but for 64-bit values.

Parameters
outPointer to receive the parsed value
sString to parse
baseNumeric base (2-36), or 0 for auto-detect (10 or 16)
strictIf true, entire string must be numeric; if false, trailing chars allowed
Returns
true on success, false on error (invalid format or out of range)