在JavaScript中转换charset

时间:2017-01-03 15:15:13

标签: javascript node.js string character-encoding

我在java中有这个文件,它可以从韩文字符集转换为UTF-8。我需要在JavaScript中使用它。我想知道节点中是否有任何库可以做到这一点。

编辑:此主题与此主题无关​​ - > How to convert character encoding from CP932 to UTF-8 in nodejs javascript, using the nodejs-iconv module (or other solution)

import java.io.UnsupportedEncodingException;

public class CharsetUtil {

    private static final String ISO_CHARSET = "ISO-8859-1";

    private static final String MS949_CHARSET = "MS949";


    /**
     * Returns the UTF-8 representation of the given ASCII string
     * 
     * @param asciiString
     * @return
     */
    public static String getUTF8String(String asciiString) {
        String utf8String = asciiString;

        if (!StringUtil.isEmpty(asciiString) && asciiString.trim().length() > 0) {
            try {
                utf8String = new String(asciiString.getBytes(ISO_CHARSET), MS949_CHARSET);
            } catch (UnsupportedEncodingException uee) {
                utf8String = null;
            }
        }

        return utf8String;
    }


    /**
     * Returns the ASCII representation of the given UTF-8 string
     * 
     * @param asciiString
     * @return
     */
    public static String getAsciiString(String utf8String) {
        String asciiString = utf8String;


        if (!StringUtil.isEmpty(utf8String) && utf8String.trim().length() > 0) {
            try {
                asciiString = new String(utf8String.getBytes(MS949_CHARSET), ISO_CHARSET);
            } catch (UnsupportedEncodingException uee) {
                asciiString = null;
            }
        }

        return asciiString;
    }


     public static void main(String[] args) {
         System.out.println("ASCII of 조달업무 is " + getAsciiString("조달업무"));
     }

}

0 个答案:

没有答案