hyeonk lab

블로그 이미지

hyeonk

hello world! hyeonk lab.

IsUtf8() - 문자열이 Utf8인지 체크하는 함수

c & c++ 2013. 8. 6. 12:28
[-] Collapse
전체 문자열을 검사하여 한 문자열이라도 utf8 코드가 들어 있는 경우 true를 리턴

bool IsUtf8(const char* str)
{
    int str_bytes = 0;
    if(str == NULL ) {
        return false;
    }
    str_bytes = (int)strlen(str);

    bool isUtf8(true);

    int index = 0;
    while (index < str_bytes && isUtf8)
    {
        char achar = str[index];
        int supplemental_bytes = 0;

        if ((achar & 0x80) == 0) {
            // 0xxxxxxx
            ++index;
        }
        else if ((achar & 0xF8) == 0xF0) {
            // 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
            ++index;
            supplemental_bytes = 3;
        }
        else if ((achar & 0xE0) == 0xE0) {
            // 1110zzzz 10yyyyyy 10xxxxxx
            ++index;
            supplemental_bytes = 2;
        }
        else if ((achar & 0xE0) == 0xC0) {
            // 110yyyyy 10xxxxxx
            ++index;
            supplemental_bytes = 1;
        }
        else {
            isUtf8 = false;
        }

        while (isUtf8 && supplemental_bytes--)
        {
            if (index >= str_bytes) {
                isUtf8 = false;
            }
            else if ((str[index++] & 0xC0) != 0x80) {
                // 10uuzzzz
                isUtf8 = false;
            }
        }
    }
    return isUtf8;
}

 

출처: http://ancdesign.tistory.com/33

저작자표시 (새창열림)

'c & c++' 카테고리의 다른 글

10년 만에 새로 태어나는 ‘모던 C++’  (0) 2014.11.04
안전한 문자열 함수들  (0) 2013.05.03
extern "C" 에 관하여...  (0) 2013.04.16
Posted by hyeonk
블로그 이미지

hello world! hyeonk lab.

by hyeonk

공지사항

    최근...

  • 포스트
  • 댓글
  • 트랙백
  • 더 보기

태그

글 보관함

«   2025/12   »
일 월 화 수 목 금 토
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

링크

카테고리

전체 (44)
ios (1)
mfc & winAPI (17)
c & c++ (4)
java (1)
eclipse (1)
visual_studio (2)
javascript (3)
asp (1)
oracle (3)
개발이야기 (5)
윈도우 일반 (2)
etc (2)
reference (2)
personal_reference (0)

카운터

Total
Today
Yesterday
방명록 : 관리자 : 글쓰기
hyeonk's Blog is powered by daumkakao
Skin info material T Mark3 by 뭐하라
favicon

hyeonk lab

hello world! hyeonk lab.

  • 태그
  • 링크 추가
  • 방명록

관리자 메뉴

  • 관리자 모드
  • 글쓰기
  • 전체 (44)
    • ios (1)
    • mfc & winAPI (17)
    • c & c++ (4)
    • java (1)
    • eclipse (1)
    • visual_studio (2)
    • javascript (3)
    • asp (1)
    • oracle (3)
    • 개발이야기 (5)
    • 윈도우 일반 (2)
    • etc (2)
    • reference (2)
    • personal_reference (0)

카테고리

PC화면 보기 티스토리 Daum

티스토리툴바