std.strtol

INTEGERstd.strtolSTRINGsINTEGERbase

Available inall subroutines.

Converts the string s to an integer value. The value base must be a constant integer expression, integer variable, or integer-returning function.

The following string formats are supported:

  • Decimal (base 10) integer syntax. For example, 123, -4.
  • Hexadecimal (base 16) integer syntax. For example, 0xABC, -0x0.
  • Octal (base 8) integer syntax. For example, 0, 0123.

The syntax for integers extends the syntax for VCL INTEGER literals in base 10 and 16 respectively. See VCL Types for details of the INTEGER literal syntax for these bases.

Supported bases are 2 to 36, inclusive, and the special value 0. For bases over 10, the alphabetic digits are case insensitive.

A base of 0 causes the base to be automatically determined from the string format. In this case, a 0x prefix indicates hex (base 16), a prefix of 0 indicates octal (base 8) and otherwise the base is taken as decimal (base 10).

When an explicit base is specified, the hexadecimal prefix of 0x and the octal prefix of 0 are not required.

Whitespace and trailing characters are permitted and have no effect on the value produced.

Errors

If the string could not be parsed, then fastly.error will be set to EPARSENUM. If the base is outside the permitted range, or if the numeric value would cause overflow, then fastly.error will be set to ERANGE.

Example

if (std.strtol(req.http.X-HexValue, 16) == 42) {
set req.http.X-TheAnswer = "Found";
}

Try it out

std.strtol is used in the following code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Use A/B testing to personalize responses

Serve different responses to separate user cohorts.