std.strtol

INTEGERstd.strtolSTRINGsINTEGERbase

Available in all 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 solution recipes, which show real world use cases. Click RUN to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

A/B testing

Serve different responses to separate user cohorts.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.