这个类是一个工具类,提供了将字符串转换为字节数组以及将字节数组转换为字符串的方法。其中在进行字节转换时,使用了 UTF-8 编码方式。

Example

const str = 'hello world'
const bytes = StringUtils.stringToByte(str) // 将字符串转换为字节数组
console.log(bytes) // 输出 [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

const str2 = StringUtils.byteToString(bytes) // 将字节数组转换为字符串
console.log(str2) // 输出 hello world

Hierarchy

  • StringUtils

Constructors

Properties

Methods

Constructors

Properties

base64js: Base64 = ...

Methods

  • 将字节数组转换为字符串

    Description

    这个方法可以将字节数组转换为对应的字符串。在实际开发中,我们可能需要从服务器上获取字节数组或者从本地文件中读取字节数组,这时就需要把字节数组转换成字符串来处理。

    Example

    const bytes = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
    const str = StringUtils.byteToString(bytes) // 将字节数组转换为字符串
    console.log(str) // 输出 hello world

    Parameters

    • arr: any

      要转换的字节数组

    Returns string

    转换后的字符串

  • 将字符串转换为字节数组

    Description

    这个方法可以将字符串转换成对应的字节数组。在实际开发中,我们可能需要将字符串发送给服务器或者保存到本地文件中,这时就需要把字符串转换成字节数组来处理。

    Example

    const str = 'hello world'
    const bytes = StringUtils.stringToByte(str) // 将字符串转换为字节数组
    console.log(bytes) // 输出 [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

    Parameters

    • str: any

      要转换的字符串

    Returns number[]

    转换后的字节数组

Generated using TypeDoc