0

I have this file in java that does convertion from Korean charset to UTF-8. I need this in JavaScript. I am wondering if there is any libraries in node that does that.

Edit: this thread has nothing to do with this one --> 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("조달업무"));
     }

}
Community
  • 1
  • 1
jimagic
  • 4,045
  • 2
  • 28
  • 49
  • @MayorMonty Did you actually read the question text? OP is asking about how to do character encoding in JavaScript and Node.js. The Java code is just for reference. – Andreas Jan 03 '17 at 15:36
  • @Andreas Geez, I didn't even see the paragraph above. Sorry OP! – bren Jan 03 '17 at 19:41
  • Possible duplicate of [How to convert character encoding from CP932 to UTF-8 in nodejs javascript, using the nodejs-iconv module (or other solution)](http://stackoverflow.com/questions/6411570/how-to-convert-character-encoding-from-cp932-to-utf-8-in-nodejs-javascript-usin) – Tom Blodget Jan 04 '17 at 02:07
  • @TomBlodget: nope that thread is different then my question – jimagic Jan 04 '17 at 15:28

0 Answers0