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

二进制字符串转到java后数据不一样 #83

Open
18si opened this issue Oct 17, 2024 · 3 comments
Open

二进制字符串转到java后数据不一样 #83

18si opened this issue Oct 17, 2024 · 3 comments

Comments

@18si
Copy link

18si commented Oct 17, 2024

java

public JSObject getMsg() {
    JSObject data = context.createNewJSObject();
    byte[] buffer = new byte[]{31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 109, -112, 79, 79, -125, 64, 20, -60, -65, 11, -90, 55, -17, -20, -82, -91, -48, 27, -108, 63, -59, -12, -45, 98, -128, 10};
    data.setProperty("msg", new String(buffer, StandardCharsets.UTF_8));
    return data;
}

public void test(String text, int length) {
    int len = text.length();
    byte[] buffer = text.getBytes(StandardCharsets.UTF_8);
    int len2 = buffer.length;
}

js

const data = getMsg()
console.log(data)
test(data.msg, data.msg.length)

结果
1729164328739

@HarlonWang
Copy link
Owner

HarlonWang commented Oct 17, 2024

下个版本会支持 byte[] 类型(预计下周),已提 PR #84 ,后面建议使用这种方式处理

@18si
Copy link
Author

18si commented Oct 17, 2024

主要是有个接口把 gzip压缩数据放到json中了,所以想在不转换的情况直接转到java结果有问题。

const rawBuf = new Uint8Array(data.msg.length);
for (let i = 0; i < data.msg.length; i++) rawBuf[i] = data.msg.charCodeAt(i);

如果字符串不支持只能这样转换了

@HarlonWang
Copy link
Owner

好的,你这个 case 我需要再看一下

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

2 participants