提示:本教程存在过时问题,更详细教程请阅读 小蓝书Typst 中文社区导航 FAQ
注意:该中文文档为社区驱动的非官网文档,可能存在错译、漏译或过时等问题,请以官网文档 Documentation 为准,如发现错漏,也欢迎 您的贡献镜像)。Typst 非官方中文交流 QQ 群:793548390
Warning: the Chinese document is a community-driven non-official document, there may be mistranslation, omission or outdated problems, please refer to the official website documentation.
Typst 中文文档

bytes

A sequence of bytes.

This is conceptually similar to an array of integers between 0 and 255, but represented much more efficiently.

You can convert

When reading data from a file, you can decide whether to load it as a string or as raw bytes.

#bytes((123, 160, 22, 0)) \
#bytes("Hello 😃")

#let data = read(
  "rhino.png",
  encoding: none,
)

// Magic bytes.
#array(data.slice(0, 4)) \
#str(data.slice(1, 4))
Preview

构造函数
如果类型具有构造函数,可以像函数一样调用它来创建一个该类型的值。

Converts a value to bytes.

bytes() -> bytes
#bytes("Hello 😃") \
#bytes((123, 160, 22, 0))
Preview

value
str bytes array
必需参数位置参数
位置参数按顺序传入,不带名称。

The value that should be converted to bytes.

定义
函数和类型可以有与其关联的定义 (成员或方法)。可以使用 "." 操作符来访问调用它们。

len

The length in bytes.

self.len(
) -> int

at

Returns the byte at the specified index. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.

self.at() -> any

index
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The index at which to retrieve the byte.

default
any

A default value to return if the index is out of bounds.

slice

Extracts a subslice of the bytes. Fails with an error if the start or index is out of bounds.

self.slice() -> bytes

start
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The start index (inclusive).

end
none int
位置参数
位置参数按顺序传入,不带名称。

The end index (exclusive). If omitted, the whole slice until the end is extracted.

默认:none

count
int

The number of items to extract. This is equivalent to passing start + count as the end position. Mutually exclusive with end.