package com.cherami;

public class GB2312 {
  public static final int SYMBOL_START=0xa1a1;
  public static final int SYMBOL_END=0xa9fe;
  public static final int FIRST_LEVEL_START=0xb0a1;
  public static final int FIRST_LEVEL_END=0xd7fe;
  public static final int SECOND_LEVEL_START=0xd8a1;
  public static final int SECOND_LEVEL_END=0xf7fe;
 
  public static boolean isSymbol(char c) {
    return isGb2312ValueRange(c,SYMBOL_START,SYMBOL_END);
  }
 
  public static boolean isFirstLevel(char c) {
    return isGb2312ValueRange(c,FIRST_LEVEL_START,FIRST_LEVEL_END);
  }
 
  public static boolean isSecondLevel(char c) {
    return isGb2312ValueRange(c,SECOND_LEVEL_START,SECOND_LEVEL_END);
  }

 
  public static boolean isGb2312ValueRange(char c,int start,int end) {
    String s= new String(new char[]{c});
    byte[] bs;
    try {
      bs = s.getBytes(“GB2312”);
    } catch (java.io.UnsupportedEncodingException e) {
      return false;
    }
    if (bs.length < 2) {
      return false;
    }
    int b1 = bs[0] & 0xff;
    int b2 = bs[1] & 0xff;
    int value = (b1 << 8) | b2;
    if (value>=start&&value<=end) {
      return true;
    }
    return false;
  }

  public static char getFirstPinyin(String str) {
    byte[] bs;
    try {
      bs = str.getBytes(“GB2312”);
    } catch (java.io.UnsupportedEncodingException e) {
      return '*';
    }

    if (bs.length == 0)
      return '*';
    if (bs.length >= 2) {
      int b1 = bs[0] & 0xff;
      int b2 = bs[1] & 0xff;
      int value = (b1 << 8) | b2;
      if (value >= 0xb0a1 && value <= 0xb0c4)
        return 'a';
      if (value >= 0xb0c5 && value <= 0xb2c0)
        return 'b';
      if (value >= 0xb2c1 && value <= 0xb4ed)
        return 'c';
      if (value >= 0xb4ee && value <= 0xb6e9)
        return 'd';
      if (value >= 0xb6ea && value <= 0xb7a1)
        return 'e';
      if (value >= 0xb7a2 && value <= 0xb8c0)
        return 'f';
      if (value >= 0xb8c1 && value <= 0xb9fd)
        return 'g';
      if (value >= 0xb9fe && value <= 0xbbf6)
        return 'h';
      if (value >= 0xbbf7 && value <= 0xbfa5)
        return 'j';
      if (value >= 0xbfa6 && value <= 0xc0ab)
        return 'k';
      if (value >= 0xc0ac && value <= 0xc2e7)
        return 'l';
      if (value >= 0xc2e8 && value <= 0xc4c2)
        return 'm';
      if (value >= 0xc4c3 && value <= 0xc5b5)
        return 'n';
      if (value >= 0xc5b6 && value <= 0xc5bd)
        return 'o';
      if (value >= 0xc5be && value <= 0xc6d9)
        return 'p';
      if (value >= 0xc6da && value <= 0xc8ba)
        return 'q';
      if (value >= 0xc8bb && value <= 0xc8f5)
        return 'r';
      if (value >= 0xc8f6 && value <= 0xcbf9)
        return 's';
      if (value >= 0xcbfa && value <= 0xcdd9)
        return 't';
      if (value >= 0xcdda && value <= 0xcef3)
        return 'w';
      if (value >= 0xcef4 && value <= 0xd188)
        return 'x';
      if (value >= 0xd189 && value <= 0xd4d0)
        return 'y';
      if (value >= 0xd4d1 && value <= 0xd7fe)
        return 'z';
    }
    return '*';
  }

}
 

(Visited 141 times, 1 visits today)