Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static byte[] unicode(String s) {
        if (s == null) {
            throw new NullPointerException();
        }

        int len = s.length();
        byte[] ret = new byte[len * 2];
        int a, b, c;
        for (int i = 0; i < len; i++) {
            c = s.charAt(i);
            a = c >> 8;// high bits
            b = c & 0xFF;// low bits
            if (a < 0) {
                a += 0xFF;
            }
            if (b < 0) {
                b += 0xFF;
            }
            ret[i * 2] = (byte) b;
            ret[i * 2 + 1] = (byte) a;
        }
        return ret;
    }

    public static String unicode(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        int len = bytes.length / 2;
        for (int i = 0; i < len; i++) {
            char c = 0;
            c |= bytes[i * 2] & 0xFF;
            c |= bytes[i * 2 + 1] << 8;
            sb.append(c);
        }
        return sb.toString();
    }
}