Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否可以用 TextEncoder 类去实现 toUtf8Bytes 函数? #11

Open
yusifeng opened this issue Feb 28, 2023 · 4 comments
Open

是否可以用 TextEncoder 类去实现 toUtf8Bytes 函数? #11

yusifeng opened this issue Feb 28, 2023 · 4 comments

Comments

@yusifeng
Copy link

yusifeng commented Feb 28, 2023

function stringToUtf8(string) {
    let encoder = new TextEncoder();
    return encoder.encode(string);
}

用这个替换 toUtf8Bytes 方法我试了下也没有问题,是出于什么原因必须要自己实现这个方法呢?

@zlyboy
Copy link
Owner

zlyboy commented Mar 3, 2023

你说的对,只是我当时写的时候没想到,现在也懒得改了,哈哈哈 ◕‿◕

@lmj01
Copy link

lmj01 commented Jan 23, 2024

我因为传的参数有点多,就说一堆数组的坐标值,会导致fixed maximum call stack size exceeded
就改成了使用TextEncoder对象来处理字符串的buffer,不使用for或...语法,使用TypedArray.prototype.set来减少遍历问题

但是真机测试存在问题!小程序本身不能使用

@zlyboy
Copy link
Owner

zlyboy commented Jan 27, 2024

能举个例子不,传了多少参数,小程序的TextEncoder网上应该有解决方案的。你可以把这里的utf8转换方式拿过来试试看https://developer.mozilla.org/zh-CN/docs/Glossary/Base64

@lmj01
Copy link

lmj01 commented Jan 31, 2024

因为TextEncoder不稳定,接口上没有明确,之前好像开放出来,后来又不行了。

我出现fixed maximum call stack size exceeded应该是convert转换时造成的,有一个数组的字符串,数组里面的又是数组,每个坐标值。在text to buffer 之前就出现了问题。我切换了思路,使用TypedArray接口来复制数据,避免了这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants