Number conversion and integer absolute value for C++ macros. More...
| int | atoi(string s) |
| double | atof(string s) |
| int | strtol(string s, int base) |
| int | abs(int i) |
| int | labs(int i) |
Number conversion and integer absolute value for C++ macros. Load with #include <stdlib.h> or #include <cstdlib> (same plugin). Available from LayoutEditor 20260918.
A string is text. These functions read that text as a number. If the text does not start with a number, you typically get 0.
Not bound (and not available here): malloc (raw memory), exit / abort (stop the whole program), system (run a shell command), qsort. For random numbers use the built-in stdLib class (stdlib::rand()).
#include <stdlib.h>
int main(){
int n = atoi("42");
double x = atof("2.5");
int a = abs(-7);
int hex = strtol("ff", 16);
cout("n=", n, " x=", x, " abs=", a, " hex=", hex, "\n");
}
Reads a decimal integer from the start of a string. Leading spaces are skipped. Reading stops at the first non-digit (so "12abc" becomes 12).
Parameters:
s (string) — text such as "42", "-3", " 17".Returns: int — the number, or 0 if nothing could be read. The range is a normal 32-bit integer (about ±2 billion).
Reads a floating-point number from the start of a string (optional sign, digits, decimal point, optional exponent like 1.5e-3).
Parameters:
s (string) — text such as "2.5", "-0.01", "1e6".Returns: double — the number, or 0 if nothing could be read.
Reads an integer written in the given base. Base 10 is ordinary decimal. Base 16 is hexadecimal (ff is 255). Base 2 is binary. Base 0 means: decimal, unless the text starts with 0x (hex) or 0 (octal).
Unlike desktop C, there is no end-pointer argument. The whole conversion uses s and base only.
Parameters:
s (string) — the text.base (int) — 0, or 2 … 36.Returns: integer (64-bit range in the plugin). strtol("ff", 16) is 255, strtol("1010", 2) is 10.
Absolute value of an integer: if i is negative, returns -i, otherwise i.
Parameters:
i (int) — a whole number.Returns: int ≥ 0. abs(-7) is 7, abs(7) is 7.
For floating-point numbers use fabs from math.h.
Absolute value for a larger (64-bit) integer. Use this when the number may not fit in a normal int.
Parameters:
i — integer (the plugin accepts a 64-bit value).Returns: the non-negative value.
Python: int(), float(), abs(). This include is for C++ macros only.