Mstdlib-1.24.0
String Conversion

Enumerations

enum  M_str_int_retval_t {
  M_STR_INT_SUCCESS = 0 ,
  M_STR_INT_OVERFLOW = 1 ,
  M_STR_INT_INVALID = 2
}
 
enum  M_str_hexdump_flags {
  M_STR_HEXDUMP_NONE = 0 ,
  M_STR_HEXDUMP_DECLEN = 1 << 0 ,
  M_STR_HEXDUMP_NOASCII = 1 << 1 ,
  M_STR_HEXDUMP_HEADER = 1 << 2 ,
  M_STR_HEXDUMP_NOLEN = 1 << 3 ,
  M_STR_HEXDUMP_CRLF = 1 << 4 ,
  M_STR_HEXDUMP_UPPER = 1 << 5 ,
  M_STR_HEXDUMP_NOSECTS = 1 << 6
}
 

Functions

long M_atofi100 (const char *s) M_WARN_UNUSED_RESULT
 
M_int64 M_atofi_prec (const char *s, int impliedDecimals) M_WARN_UNUSED_RESULT
 
M_int64 M_str_to_int64 (const char *s) M_WARN_UNUSED_RESULT
 
M_uint64 M_str_to_uint64 (const char *s) M_WARN_UNUSED_RESULT
 
M_str_int_retval_t M_str_to_int64_ex (const char *s, size_t len, unsigned char base, M_int64 *val, const char **endptr)
 
M_str_int_retval_t M_str_to_uint64_ex (const char *s, size_t len, unsigned char base, M_uint64 *val, const char **endptr)
 
M_str_int_retval_t M_str_to_int32_ex (const char *s, size_t len, unsigned char base, M_int32 *val, const char **endptr)
 
M_str_int_retval_t M_str_to_uint32_ex (const char *s, size_t len, unsigned char base, M_uint32 *val, const char **endptr)
 
M_int32 M_str_to_int32 (const char *s) M_WARN_UNUSED_RESULT
 
M_uint32 M_str_to_uint32 (const char *s) M_WARN_UNUSED_RESULT
 
char * M_str_dot_money_out (const char *amount)
 
char * M_str_hexdump (int flags, size_t bytes_per_line, const char *line_prefix, const unsigned char *data, size_t data_len)
 

Detailed Description

String Conversion

Enumeration Type Documentation

◆ M_str_int_retval_t

Possible return codes for integer conversion primitives

Enumerator
M_STR_INT_SUCCESS 

Successful conversion

M_STR_INT_OVERFLOW 

Overflow

M_STR_INT_INVALID 

Invalid Characters

◆ M_str_hexdump_flags

Hex dump flags

Enumerator
M_STR_HEXDUMP_NONE 

Defaults

M_STR_HEXDUMP_DECLEN 

Default is length in hex (address) format, print in decimal format instead

M_STR_HEXDUMP_NOASCII 

Disable dumping of ASCII representation trailing the hexdump

M_STR_HEXDUMP_HEADER 

Add a header above each column of output

M_STR_HEXDUMP_NOLEN 

Omit the length indicator

M_STR_HEXDUMP_CRLF 

Use CRLF newlines (DOS style)

M_STR_HEXDUMP_UPPER 

Output hex digits as uppercase

M_STR_HEXDUMP_NOSECTS 

Do not put additional emphasis on 8-byte segments

Function Documentation

◆ M_atofi100()

long M_atofi100 ( const char *  s)

Convert a string representing money (a fractional decimal amount) to an integer number of cents. Fractional amounts are rounded to the nearest cent.

Parameters
sString containing a floating point (2 decimal places) money representation.
Returns
Long integer, rounded if there are floating point errors, with amount of cents.

◆ M_atofi_prec()

M_int64 M_atofi_prec ( const char *  s,
int  impliedDecimals 
)

Convert a floating point number into a 64bit integer using arbitrary precision as defined by impliedDecimals.

For instance, if impliedDecimals is 5, and 12.34 is passed, the resulting value would be 1234000.

Parameters
[in]sstring to convert to decimal
[in]impliedDecimalsNumber of implied decimals resulting output should have
Returns
64bit integer representing number from s with implied decimals.

◆ M_str_to_int64()

M_int64 M_str_to_int64 ( const char *  s)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
Returns
Determined integer. On failure will return 0 which cannot be differentiated from a legitimate 0.

◆ M_str_to_uint64()

M_uint64 M_str_to_uint64 ( const char *  s)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
Returns
Determined integer. On failure will return 0 which cannot be differentiated from a legitimate 0.

◆ M_str_to_int64_ex()

M_str_int_retval_t M_str_to_int64_ex ( const char *  s,
size_t  len,
unsigned char  base,
M_int64 *  val,
const char **  endptr 
)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
[in]lenMaximum length of given string to parse.
[in]baseValid range 2 - 36. 0 to autodetect based on input (0x = hex, 0 = octal, anything else is decimal).
[out]valInteger to store result.
[out]endptrPointer to store the end of the parsed string.
Returns
One of M_str_int_retval_t return codes.

◆ M_str_to_uint64_ex()

M_str_int_retval_t M_str_to_uint64_ex ( const char *  s,
size_t  len,
unsigned char  base,
M_uint64 *  val,
const char **  endptr 
)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
[in]lenMaximum length of given string to parse.
[in]baseValid range 2 - 36. 0 to autodetect based on input (0x = hex, 0 = octal, anything else is decimal).
[out]valInteger to store result.
[out]endptrPointer to store the end of the parsed string.
Returns
One of M_str_int_retval_t return codes.

◆ M_str_to_int32_ex()

M_str_int_retval_t M_str_to_int32_ex ( const char *  s,
size_t  len,
unsigned char  base,
M_int32 *  val,
const char **  endptr 
)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
[in]lenMaximum length of given string to parse.
[in]baseValid range 2 - 36. 0 to autodetect based on input (0x = hex, 0 = octal, anything else is decimal).
[out]valInteger to store result.
[out]endptrPointer to store the end of the parsed string.
Returns
One of M_str_int_retval_t return codes.

◆ M_str_to_uint32_ex()

M_str_int_retval_t M_str_to_uint32_ex ( const char *  s,
size_t  len,
unsigned char  base,
M_uint32 *  val,
const char **  endptr 
)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
[in]lenMaximum length of given string to parse.
[in]baseValid range 2 - 36. 0 to autodetect based on input (0x = hex, 0 = octal, anything else is decimal).
[out]valInteger to store result
[out]endptrPointer to store the end of the parsed string.
Returns
One of M_str_int_retval_t return codes.

◆ M_str_to_int32()

M_int32 M_str_to_int32 ( const char *  s)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
Returns
determined integer. On failure will return 0 which cannot be differentiated from a legitimate 0.

◆ M_str_to_uint32()

M_uint32 M_str_to_uint32 ( const char *  s)

Interpret a string as an ascii numeric. String may begin with whitespace which will be ignored, then an optional + or - sign.

Parameters
[in]sNULL-terminated string.
Returns
determined integer. On failure will return 0 which cannot be differentiated from a legitimate 0.

◆ M_str_dot_money_out()

char * M_str_dot_money_out ( const char *  amount)

Verify and convert the amount so it always has 2 decimal digits.

Example: 1 for $1 and turns it into 1.00. 1.1 for $1.10 and turns it into 1.10.

Parameters
[in]amountAmount to verify/convert.
Returns
Amount with decimal and two decimal digits. Or NULL on error.

◆ M_str_hexdump()

char * M_str_hexdump ( int  flags,
size_t  bytes_per_line,
const char *  line_prefix,
const unsigned char *  data,
size_t  data_len 
)

Generate a hex dump format of binary data meant to be human-readable, or imported via various hex-dump conversion tools such as Text2pcap.

Parameters
flagsone or more enum M_str_hexdump_flags
bytes_per_lineNumber of bytes represented per line. If zero is used, defaults to 16
line_prefixPrefix each line of the hex dump with the given data.
dataBinary data to be dumped
data_lenlength of binary data to be dumped
Returns
Allocated string representing the hex dump. Must be M_free()'d by the caller.