Java - Write code to convert String to Unicode

Requirements

Write code to convert String to Unicode

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com";
        System.out.println(toUnicode(s));
    }//from   w w w  . j  a va2  s. c  o  m

    public static String toUnicode(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); ++i)
            sb.append(toUnicode(s.charAt(i)));
        return sb.toString();
    }

    public static String toUnicode(char c) {
        return "\\u" + Integer.toHexString(c | 0x10000).substring(1);
    }
}