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 functions for converting string representations of numbers to their corresponding integer and floating-point values. The implementation handles:

  • Leading/trailing whitespace
  • Optional sign indicators (+/-)
  • Decimal points for floating numbers
  • Partial number parsing (stops at first invalid character)
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

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.

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 string to 32-bit signed integer.

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