简体   繁体   English

如何将Unicode JavaScript字符串转换为字节数组?

[英]How to convert an unicode JavaScript string into a byte array?

I have a service writing remote files, but it requires a byte array as input. 我有写远程文件的服务,但是它需要一个字节数组作为输入。 Rest of the interface provides only JavaScript unicode strings. 该接口的其余部分仅提供JavaScript unicode字符串。 No way to write them then. 那没办法写。

I found something like this in MDC: 我在MDC中发现了以下内容:

var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
                .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
var s = {};
var tt = 'test string';
var data = converter.convertToByteArray(tt, s);

According to what they say in MDC, this should do exactly what I need, but it fails with this: 根据他们在MDC中所说的,这应该可以完全满足我的需要,但是这样做失败:

Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIScriptableUnicodeConverter.convertToByteArray] 组件返回的失败代码:0x80004005(NS_ERROR_FAILURE)[nsIScriptableUnicodeConverter.convertToByteArray]

In docs there is the string must not be UTF-16, and I've read JS uses UTF-16 by default. 在文档中,该字符串不能为UTF-16,并且我读过JS默认情况下使用UTF-16。

Any other ways to produce this damn byte array from string? 还有其他方法可以从字符串生成该死的字节数组吗?

You must assing the charset! 你必须挑这个字符集! For example: converter.charset = 'UTF-8'; 例如:converter.charset ='UTF-8';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM