1 /**
   2  * @(#)WhichEncoding.java    16-Mar-2001
   3  *
   4  *  isEucKr(String) returns true if String contains any one of euc-kr chars,
   5  *  but returns false otherwise.
   6  *
   7  *  isEucKr(String) 은 String 에 한글이 한자라도 있으면 true 를,
   8  *  아니면 false  를 리턴한다.
   9  *
  10  *  Caution!!
  11  *     This could work incorrectly for Hanja and Symbol characters.
  12  *
  13  *  주의!!
  14  *     한자와 기호문자가 있는 String 은 부정확하게 처리될 수 있음.
  15  *     하지만 7비트 문자(즉, 0x0001 ~ 0x007F 범위 내의 문자) 아닌 문자 중에
  16  *     처음 나타나는 문자가 한글(즉, 0xAC00 ~ 0xD7A3 범위 내의 문자)인
  17  *     경우에는 정확하게 처리된다.
  18  *
  19  * @date    16-Mar-2001
  20  * @author  Pilho Kim   [phkim@cluecom.co.kr]
  21  */
  22 
  23 public class WhichEncoding {
  24   public static String toHex(char a) {
  25     char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  26                    'A', 'B', 'C', 'D', 'E', 'F' };
  27     int x = a & 0xFFFF;
  28     int x1 = (x >>> 12) & 0x0F;
  29     int x2 = (x >>> 8) & 0x0F;
  30     int x3 = (x >>> 4) & 0x0F;
  31     int x4 = x & 0x0F;
  32 
  33     return "" + hex[x1] + hex[x2] + hex[x3] + hex[x4];
  34   }
  35 
  36   public static boolean isEucKr(String s) {
  37     int len = s.length();
  38     char c;
  39     for (int i = 0; i < len; i++) {
  40       c = s.charAt(i);
  41       /// System.out.println("" + c + " = " + toHex(c));
  42       if (((c & 0xFFFF) >= 0xAC00) && ((c & 0xFFFF) <= 0xD7A3))
  43         return true;
  44       /// else if (((c & 0xFF00) != 0) && ((c & 0x00) == 0))
  45       ///   return false;
  46     }
  47     return false;
  48   }
  49 
  50   public static boolean isISO8859(String s) {
  51     int len = s.length();
  52     char c;
  53     for (int i = 0; i < len; i++) {
  54       c = s.charAt(i);
  55       /// System.out.println("" + c + " = " + toHex(c));
  56       if ((c & 0xFF00) != 0)
  57         return false;
  58     }
  59     return true;
  60   }
  61 
  62   public static void main(String[] args) {
  63     try {
  64       // String a = "한글 Test";
  65       String a = " a ± b (단, ±는 복호 동순)";
  66       String b = new String(a.getBytes("euc-kr"), "8859_1");
  67       System.out.println("isEucKr(\"" + a + "\") --> " + isEucKr(a));
  68       System.out.println("isEucKr(\"" + b + "\") --> " + isEucKr(b));
  69       String c = " a ± b";
  70       System.out.println("isISO8859(\"" + c + "\") --> " + isISO8859(c));
  71     }
  72     catch (java.io.UnsupportedEncodingException ex) {
  73     }
  74   }
  75 }


CategoryJava

ZbmonWiki: WhichEncoding.java (2005-11-07 14:27:38에 zbmon가(이) 마지막으로 수정)