DB/Oracle

OraCon

swhwang 2016. 5. 4. 23:41

import java.sql.SQLException;

public class OraCon {

public static String toDB( String uni20 )throws SQLException {    

    if ( uni20 == null )

            return null;

          int len = uni20.length();

          char[] out = new char[len];


          for( int i = 0; i < len; i++ ) {

            char c = uni20.charAt(i);


            if ( c < 0xac00 || 0xd7a3 < c ) {

                out[i] = c;

            } else  { // 유니코드 2.0 한글 영역

                            try {

                    byte[] ksc = String.valueOf(c).getBytes("KSC5601");

                    if ( ksc.length != 2 ) {

                        out[i] = '\ufffd';

                        System.err.println("Warning: Some of Unicode 2.0 hangul character  was ignored." );

                    } else {

                        out[i] = (char) ( 0x3400 +

                                   ((ksc[0] & 0xff) - 0xb0) * 94 +

                                    (ksc[1] & 0xff) - 0xa1 );

                    }

                } catch( java.io.UnsupportedEncodingException ex ) {   

throw new SQLException( ex.getMessage() );

                }

                }

        }

        return new String( out );

     }


     // Unicode 1.2 -> Unicode 2.0

     public static String fromDB( String uni12 ) throws SQLException {

        if ( uni12 == null )

return null;


          int len = uni12.length();

          char[] out = new char[len];

          byte[] ksc = new byte[2];


        for( int i = 0; i < len; i++ ) {

              char c = uni12.charAt(i);


              if ( c < 0x3400 || 0x4dff < c ) {

            out[i] = c;

              } else if ( 0x3d2e <= c ) { //유니코드 1.2 한글 보충 영역 A, B

              System.err.println(

                "Warning: Some of Unicode 1.2 hangul character was ignored." );

            out[i] = '\ufffd';

              } else { // 유니코드 1.2의 KSC5601 대응 한글 영역

                          try {

                  ksc[0] = (byte) ( (c - 0x3400) / 94 + 0xb0 );

                  ksc[1] = (byte) ( (c - 0x3400) % 94 + 0xa1 );

                  out[i] = new String( ksc, "KSC5601" ).charAt(0);

            } catch( java.io.UnsupportedEncodingException ex ) {  

throw new SQLException( ex.getMessage() );

                }

            }

          }

          return new String( out );

  }

}