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

String to number conversion implementation. More...

#include "utils/conversion.h"
Include dependency graph for conversion.c:

Functions

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

Detailed Description

String to number conversion implementation.

This file provides the implementation for converting string representations of numbers to their corresponding integer and floating-point values. The functions handle various formats including:

  • Leading/trailing whitespace
  • Optional sign indicators
  • Decimal points for floating numbers
  • Partial number parsing
Note
These are blocking implementations that parse until first invalid character
No overflow/underflow checking is performed
Does not support scientific notation or hexadecimal formats
Author
Ashutosh Vishwakarma
Date
2025-07-23

Function Documentation

◆ str_to_float()

float str_to_float ( const char * s)

Convert string to floating-point number.

Convert a string to a floating-point number.

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
Parameters
sPointer to null-terminated input string
Returns
Converted floating-point value
Warning
Limited precision for fractional components
No overflow/underflow protection

Example usage:

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:96

◆ str_to_int()

int32_t str_to_int ( const char * s)

Convert string to 32-bit signed integer.

Convert a string to a 32-bit signed integer.

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
Parameters
sPointer to null-terminated input string
Returns
Converted 32-bit integer value
Warning
Potential for overflow with large numbers
No error reporting for invalid inputs

Example usage:

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:44