전각문자, 반각문자 정리 (Java)

Simple is Best·2021년 6월 3일
0

source 는 Github 에 있습니다.

반각, 전각 문자 개념

  • 전각 문자는 정사각형 형태, 반각은 전각의 가로폭을 반으로 줄인 직사각형 형태입니다.
  • 영문과 숫자 등은 반각이고, 한글은 전각으로 표현하고 있습니다.
  • 컴퓨터에서 보통 반각은 1바이트, 전각은 2바이트로 표현됩니다. (유니코드 등장하기 전까지)
    • UTF-8, UTF-16 의 경우 반각 1bytes, 전각 2bytes 가 아닐 수 있습니다.
  • 업무 개발을 할 때, 보통 반각문자를 접하지만 가끔 전각문자를 접하기도 합니다. 그래서 정리하게 됐습니다.
    • 반각문자가 1bytes, 전각문자가 2bytes 인 인코딩에 대해서 정리하겠습니다. (euc-kr)

전각문자 관련 source

  • 전각문자가 2byte 인지 확인하고 길이가 1인지 체크하는 source 입니다.
  • 인코딩이 euc-kr 로 설정돼있어서 전각문자를 0xA1, 0xA1 을 전각문자 공백으로 인식합니다.

    @Test
    public void 전각문자테스트() throws Exception {
        // 전각문자 공백 (0xA1 0xA1)
        byte[] bytes = {(byte)0xA1, (byte)0xA1};
        Assert.assertThat(bytes.length, is(2));
        String s = new String(bytes, Charset.forName("euc-kr"));
        // 2개의 바이트를 읽어서 하나의 전각문자로 만드니 길이가 1
        Assert.assertThat(s.length(), is(1));
        byte[] tmp = s.getBytes(Charset.forName("euc-kr"));
        // 전각문자를 bytes 변환하면 1개의 전각문자는 2byte
        Assert.assertThat(tmp.length, is(2));
    }

반각문자, 전각문자 변환 source


    /**
     * 반각문자 --> 전각문자 변환
     *
     * @param s
     * @return
     */
    public static String toFullWidthCharacter(String s) {
        if (s == null) {
            throw new IllegalArgumentException("s is null");
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            // 영문 알파벳 or 특수 문자
            if (c >= 0x21 && c <= 0x7e) {
                c += 0xfee0;
            }
            // 공백 (=space)
            else if (c == 0x20) {
                c = 0x3000;
            }
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 전각문자를 반각문자로 변환
     * @param s
     * @return
     */
    public static String toHalfWidthCharacter(String s) {
        if (s == null) {
            throw new IllegalArgumentException("s is null");
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            // 영문 알파벳 or 특수 문자
            if (c >= '!' && c <= '~') {
                c -= 0xfee0;
            }
            // 공백 (=space)
            else if (c == ' ') {
                c = 0x20;
            }
            sb.append(c);
        }
        return sb.toString();
    }


    @Test
    public void 반각문자_전각문자_변환_테스트() throws Exception {
        String s = " ";
        // 반각문자 공백이기에 1bytes
        Assert.assertThat(s.getBytes(Charset.forName("euc-kr")).length, is(1));

        String fullWidthCharacter = StringUtils.toFullWidthCharacter(s);
        // 전각문자 공백이기에 2bytes
        Assert.assertThat(fullWidthCharacter.getBytes("euc-kr").length, is(2));

        String halfWidthCharacter = StringUtils.toHalfWidthCharacter(fullWidthCharacter);
        // 반각문자 bytes 이기에 byte 길이 1
        Assert.assertThat(halfWidthCharacter.getBytes("euc-kr").length, is(1));
    }

reference

profile
Simple is best

0개의 댓글