NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
conversion.h File Reference

String to number conversion utilities interface. More...

#include <stdint.h>
Include dependency graph for conversion.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int32_t str_to_int (const char *s)
 Convert a string to a 32-bit signed integer.
float str_to_float (const char *s)
 Convert a string to a floating-point number.

Detailed Description

String to number conversion utilities interface.

This header declares functions for converting string representations of numbers to their corresponding integer and floating-point values. The implementations handle:

  • Leading/trailing whitespace
  • Optional sign indicators (+/-)
  • Decimal points for floating-point numbers
  • Partial number parsing (stops at first invalid character)
Note
These functions do not handle scientific notation or hexadecimal formats
No overflow/underflow checking is performed
Author
Ashutosh Vishwakarma
Date
2025-07-23

Function Documentation

◆ str_to_float()

float str_to_float ( const char * s)

Convert a string to a floating-point number.

Parameters
sNull-terminated string to convert (may contain leading whitespace)
Returns
Converted floating-point value
Warning
Limited precision for fractional components
Stops parsing at first non-digit/non-dot character

Convert a string to a floating-point number.

Parameters
[in]sPointer to null-terminated input string
Returns
Converted floating-point value

Parses the input string to construct a float value:

  1. Skips leading whitespace
  2. Processes optional sign
  3. Handles both integer and fractional parts
  4. Properly tracks decimal point position
  5. Stops at first invalid character
Warning
Limited precision for fractional components
No overflow/underflow protection
float val = str_to_float(" 3.1415xyz"); // Returns 3.1415f
float str_to_float(const char *s)
Convert a string to a floating-point number.
Definition conversion.c:87

◆ str_to_int()

int32_t str_to_int ( const char * s)

Convert a string to a 32-bit signed integer.

Parameters
sNull-terminated string to convert (may contain leading whitespace)
Returns
Converted 32-bit integer value
Warning
Potential integer overflow with large numbers
Stops parsing at first non-digit character

Convert a string to a 32-bit signed integer.

Parameters
[in]sPointer to null-terminated input string
Returns
Converted 32-bit integer value

Parses the input string character by character to construct the integer value:

  1. Skips any leading whitespace (spaces/tabs)
  2. Processes optional sign character (+/-)
  3. Converts subsequent digit characters to integer value
  4. Stops at first non-digit character
Warning
Potential for overflow with large numbers
No error reporting for invalid inputs
int val = str_to_int(" -1234abc"); // Returns -1234
int32_t str_to_int(const char *s)
Convert a string to a 32-bit signed integer.
Definition conversion.c:38