Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    private static int compareToGBK(String s1, String s2) {
        int ret = 0;
        try {
            byte[] bytes1 = s1.getBytes("gbk");
            byte[] bytes2 = s2.getBytes("gbk");

            int len = Math.min(bytes1.length, bytes2.length);
            for (int i = 0; i < len; i++) {

                if (bytes1[i] > 0 && bytes2[i] > 0) {
                    ret = Character.toLowerCase(bytes1[i]) - Character.toLowerCase(bytes2[i]);
                    if (ret == 0)
                        ret = bytes1[i] - bytes2[i];
                } else {
                    int b1 = (bytes1[i] + 256) % 256;
                    int b2 = (bytes2[i] + 256) % 256;
                    ret = b1 - b2;
                }

                if (ret != 0) {
                    break;
                }

            }
            if (ret == 0) {
                ret = bytes1.length - bytes2.length;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return ret;
    }
}