本文介绍了如何在nodejs javascript中将字符编码从CP932转换为UTF-8,使用nodejs-iconv模块(或其他解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个字符串从CP932(aka Windows-31J)转换为utf8在javascript中。基本上我是爬行忽略了请求头中utf-8的请求,并返回CP932编码的文本(即使HTML元标记表明该网页是shift_jis访问)的网站。

I'm attempting to convert a string from CP932 (aka Windows-31J) to utf8 in javascript. Basically I'm crawling a site that ignores the utf-8 request in the request header and returns cp932 encoded text (even though the html metatag indicates that the page is shift_jis).

无论如何,我有整个页面存储在一个字符串变量html。从那里,我试图使用这个代码转换为utf8:

Anyway, I have the entire page stored in a string variable called "html". From there I'm attempting to convert it to utf8 using this code:

var Iconv = require('iconv').Iconv;
var conv = new Iconv('CP932', 'UTF-8//TRANSLIT//IGNORE');

var myBuffer = new Buffer(html.length * 3);
myBuffer.write(html, 0, 'utf8')
var utf8html = (conv.convert(myBuffer)).toString('utf8');

结果不是它应该是。例如,字符串:投稿者さんの稚内全日空ホテルのクチコミ(感想·情报)出来为ソスソスソスËソスメゑソスソスソスソスソスソス牛逼ソスソスソス小号ソスソスソスソスソスžソスËソスソスソスフクソス`ソス - [Rソス〜(ソスソスソスžソスËソスソスソスソス)

The result is not what it's supposed to be. For example, the string: "投稿者さんの 稚内全日空ホテル のクチコミ (感想・情報)" comes out as "ソスソスソスeソスメゑソスソスソスソスソス ソスtソスソスソスSソスソスソスソスソスzソスeソスソス ソスフクソス`ソスRソス~ (ソスソスソスzソスEソスソスソスソス)"

如果我删除// TRANSLIT //忽略(这应该引起它的缺字返回类似字符,如果做不到这一点忽略非转码,能够字符),我得到这个错误:
错误:EILSEQ,非法字符序列

If I remove //TRANSLIT//IGNORE (Which should cause it to return similar characters for missing characters, and failing that omit non-transcode-able characters), I get this error:Error: EILSEQ, Illegal character sequence.

我愿意用任何,可以在被的NodeJS实现的解决方案,但我的搜索结果都没有取得的NodeJS-的iconv模块以外的许多选项

I'm open to using any solution that can be implemented in nodejs, but my search results haven't yielded many options outside of the nodejs-iconv module.

的NodeJS-REF的iconv:的

nodejs-iconv ref: https://github.com/bnoordhuis/node-iconv

谢谢!

编辑24.06.2011:
我一直在使用Java实现一个解决方案。但是,如果有人能解决这个问题,我仍然会对JavaScript解决方案感兴趣。

Edit 24.06.2011:I've gone ahead and implemented a solution in Java. However I'd still be interested in a javascript solution to this problem if somebody can solve it.

推荐答案

:)

它取决于libiconv。您需要libiconv-1.13-ja-1.patch。

请检查以下内容。

I got same trouble today :)
It depends libiconv. You need libiconv-1.13-ja-1.patch.
Please check followings.

  • http://d.hatena.ne.jp/ushiboy/20110422/1303481470
  • http://code.xenophy.com/?p=1529

或者您可以避免使用iconv-jp尝试

or you can avoid problem using iconv-jp try

npm install iconv-jp

这篇关于如何在nodejs javascript中将字符编码从CP932转换为UTF-8,使用nodejs-iconv模块(或其他解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:24