Miscellaneous functions

更新时间: 2026-04-12 01:29:34

This topic describes the syntax, features, parameters, and return values of miscellaneous functions. This topic also provides examples of these functions.

base64_enc | base64_dec | url_escape | url_unescape | randomseed | rand | rand_hit | crc | tonumber | base64_enc_safe | base64_dec_safe

base64_enc

Item

Description

Syntax

base64_enc(s, [no_padding]).

Description

Base64 encoding.

Parameters

  • s: The string to encode.

  • no_padding (optional): Specifies whether to use padding. A value of true disables padding. The default value is false.

Return value

The Base64-encoded string.

Example

if $http_data {
 decdata = base64_dec($http_data)
 say(concat('base64_decdata=', decdata))
 say(concat('base64_encdata=', base64_enc('hello, dsl'))) 
}

Request header: "data: aGVsbG8sIGRzbA=="

Response:

base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==

base64_dec

Item

Description

Syntax

base64_dec(s).

Description

Decodes a Base64-encoded string.

Parameters

s: The string to decode.

Return value

The decoded string.

Example

if $http_data {
 decdata = base64_dec($http_data)
 say(concat('base64_decdata=', decdata))
 say(concat('base64_encdata=', base64_enc('hello, dsl'))) 
}

Request header: "data: aGVsbG8sIGRzbA=="

Response:

base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==

url_escape

Item

Description

Syntax

url_escape(s).

Description

URL encoding.

Parameters

s: The string to encode.

Return value

The URL-encoded string.

Example

raw = '/abc/123/ dd/file.m3u8'
esdata = url_escape(raw)
dsdata = url_unescape(esdata)
if eq(raw, dsdata) {
  say(concat('raw=', raw))
  say(concat('esdata=', esdata))
  say(concat('dsdata=', dsdata))
}

Outputs:

raw=/abc/123/ dd/file.m3u8
dsdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8

url_unescape

Item

Description

Syntax

url_unescape(s) .

Description

Decodes a URL-encoded string.

Parameters

s: The string to decode.

Return value

Returns a URL-decoded string.

Example

raw = '/abc/123/ dd/file.m3u8'
esdata = url_escape(raw)
dsdata = url_unescape(esdata)
if eq(raw, dsdata) {
  say(concat('raw=', raw))
  say(concat('esdata=', esdata))
  say(concat('dsdata=', dsdata))
}

Outputs:

raw=/abc/123/ dd/file.m3u8
esdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8

randomseed

Item

Description

Syntax

randomseed().

Description

Specifies a seed for random number generation.

Parameters

None.

Return value

None.

Example

randomseed()
r = rand(1,100)

rand

Item

Description

Syntax

rand(n1, n2).

Description

Generates a random number. The number is between n1 and n2, inclusive.

Parameters

  • n1: The lower bound of the random number.

  • n2: The upper bound of the random number.

Return value

This operation returns the generated random number.

Example

r = rand(1,100)

rand_hit

Item

Description

Syntax

rand_hit(ratio).

Description

Returns true or false based on a specified probability.

Parameters

ratio: The probability of returning true. The value must be between 0 and 100.

Return value

Returns true with the probability specified by ratio. For example, if ratio is 100, the function returns true. If ratio is 0, the function returns false.

Example

rand_hit(80)

crc

Item

Description

Syntax

crc(s).

Description

Calculates a Cyclic Redundancy Check (CRC) digest.

Parameters

s: The string for which to calculate the CRC digest.

Return value

The CRC digest of the string s.

Example

crc('hello ascript')

tonumber

Item

Description

Syntax

tonumber(s, [base]).

Description

Converts a string to a number.

Parameters

  • s: The string to convert.

  • base (Optional): The target conversion base. Valid values: 10 and 16. The default is 10.

Example

n = tonumber('100')
say(concat('tonumber()=', n))

Output: tonumber()=100

base64_enc_safe

Item

Description

Syntax

base64_enc_safe(str).

Description

Performs URL-safe Base64 encoding on a string. In the output, `+` is replaced with `-`, `/` is replaced with `_`, and any trailing `=` padding is removed.

Parameters

str: The string to encode.

Return value

Returns a string.

Example

add_rsp_header('X-RESPOND-OUTPUT', concat('base64_enc_safe=', base64_enc_safe('hello, dsl')), true)

Response header:

X-RESPOND-OUTPUT: base64_enc_safe=aGVsbG8sIGRzbA

base64_dec_safe

Item

Description

Syntax

base64_dec_safe(str).

Description

Decodes a URL-safe Base64-encoded string. Before decoding, `-` is replaced with `+`, `_` is replaced with `/`, and the string is padded with `=` at the end to a multiple of 4 characters.

Parameters

str: The URL-safe Base64-encoded content.

Return value

Returns a string.

Example

add_rsp_header('X-RESPOND-OUTPUT', concat('base64_dec_safe=', base64_dec_safe(base64_enc_safe('hello, dsl'))), true)

Response header:

X-RESPOND-OUTPUT:base64_dec_safe=hello, dsl
上一篇: JSON functions 下一篇: Array functions
阿里云首页 负载均衡 相关技术圈