Q&A

  • UTF-8로 엔코딩된 문자열을 어떻게 ANSI로 바꾸나요?
UTF-8로 엔코딩 되어서 한글을 읽을수가 없어요...
이를 읽을수 있는 ANSI로 바꾸는 방법좀 가르쳐 주세요.
아무리 봐도 없고 솔직히 잘 마르겠어요.
고수님들의 조언 부탁드립니다.
3  COMMENTS
  • Profile
    유성웅 2002.04.12 01:38
    이건 어떼요

    MultiByteToWideChar

    The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.

    int MultiByteToWideChar(
      UINT CodePage,         // code page
      DWORD dwFlags,         // character-type options
      LPCSTR lpMultiByteStr, // string to map
      int cbMultiByte,       // number of bytes in string
      LPWSTR lpWideCharStr,  // wide-character buffer
      int cchWideChar        // size of buffer
    );Parameters

    CodePage
    [in] Specifies the code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system. You can also specify one of the values shown in the following table. Value Meaning
    CP_ACP ANSI code page
    CP_MACCP Macintosh code page
    CP_OEMCP OEM code page
    CP_SYMBOL Windows 2000/XP: Symbol code page (42)
    CP_THREAD_ACP Windows 2000/XP: The current thread's ANSI code page
    CP_UTF7 Windows 98/Me, Windows NT 4.0 and later: Translate using UTF-7
    CP_UTF8 Windows 98/Me, Windows NT 4.0 and later: Translate using UTF-8. When this is set, dwFlags must be zero.


    Windows 95: Under the Microsoft Layer for Unicode, MultiByteToWideChar also supports CP_UTF7 and CP_UTF8.

    dwFlags
    [in] Indicates whether to translate to precomposed or composite-wide characters (if a composite form exists), whether to use glyph characters in place of control characters, and how to deal with invalid characters. You can specify a combination of the following flag constants. Value Meaning
    MB_PRECOMPOSED Always use precomposed characters—that is, characters in which a base character and a nonspacing character have a single character value. This is the default translation option. Cannot be used with MB_COMPOSITE.
    MB_COMPOSITE Always use composite characters—that is, characters in which a base character and a nonspacing character have different character values. Cannot be used with MB_PRECOMPOSED.
    MB_ERR_INVALID_CHARS If the function encounters an invalid input character, it fails and GetLastError returns ERROR_NO_UNICODE_TRANSLATION.
    Note that this flag is not supported for DLL-based encodings.

    MB_USEGLYPHCHARS Use glyph characters instead of control characters.


    A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/nonspacing character combination. In the character č, the e is the base character and the accent grave mark is the nonspacing character.

    The function's default behavior is to translate to the precomposed form. If a precomposed form does not exist, the function attempts to translate to a composite form.

    The flags MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. The MB_USEGLYPHCHARS flag and the MB_ERR_INVALID_CHARS can be set regardless of the state of the other flags.

    For the code pages in the following table, dwFlags must be zero, otherwise the function fails with ERROR_INVALID_FLAGS. 50220
    50221

    50222

    50225
    50227
    50229

    52936

    54936
    57002 through 57011
    65000 (UTF7)

    65001 (UTF8)




    lpMultiByteStr
    [in] Points to the character string to be converted.
    cbMultiByte
    [in] Specifies the size in bytes of the string pointed to by the lpMultiByteStr parameter, or it can be -1 if the string is null terminated.
    If this parameter is -1, the function processes the entire input string including the null terminator. The resulting wide character string therefore has a null terminator, and the returned length includes the null terminator.

    If this parameter is a positive integer, the function processes exactly the specified number of bytes. If the given length does not include a null terminator then the resulting wide character string will not be null terminated, and the returned length does not include a null terminator.

    lpWideCharStr
    [out] Points to a buffer that receives the translated string.
    cchWideChar
    [in] Specifies the size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter. If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer.
    Return Values
    If the function succeeds, and cchWideChar is nonzero, the return value is the number of wide characters written to the buffer pointed to by lpWideCharStr.

    If the function succeeds, and cchWideChar is zero, the return value is the required size, in wide characters, for a buffer that can receive the translated string.

    If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError may return one of the following error codes:

    ERROR_INSUFFICIENT_BUFFER
    ERROR_INVALID_FLAGS
    ERROR_INVALID_PARAMETER
    ERROR_NO_UNICODE_TRANSLATION

    Remarks
    The lpMultiByteStr and lpWideCharStr pointers must not be the same. If they are the same, the function fails, and GetLastError returns the value ERROR_INVALID_PARAMETER.

    The function fails if MB_ERR_INVALID_CHARS is set and encounters an invalid character in the source string. An invalid character is either, a) a character that is not the default character in the source string but translates to the default character when MB_ERR_INVALID_CHARS is not set, or b) for DBCS strings, a character which has a lead byte but no valid trailing byte. When an invalid character is found, and MB_ERR_INVALID_CHARS is set, the function returns 0 and sets GetLastError with the error ERROR_NO_UNICODE_TRANSLATION.

    Windows XP: To prevent the security problem of the non-shortest-form versions of characters, MultiByteToWideChar deletes these characters.

    Windows 95/98/Me: MultiByteToWideChar is supported by the Microsoft Layer for Unicode. To use this version, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

    Example Code
    For an example, see Looking Up a User's Full Name.

    Requirements
      Windows NT/2000/XP: Included in Windows NT 3.1 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Winnls.h; include Windows.h.
      Library: Use Kernel32.lib.

    See Also
    Unicode and Character Sets Overview, Unicode and Character Set Functions, WideCharToMultiByte

  • Profile
    이준희 2002.03.07 18:30
    CONST
      // --- Character Translation Table for Unicode <-> Win-1252
      WIN1252_UNICODE : ARRAY [$00..$FF] OF WORD = (
                        $0000, $0001, $0002, $0003, $0004, $0005, $0006, $0007, $0008, $0009,
                        $000A, $000B, $000C, $000D, $000E, $000F, $0010, $0011, $0012, $0013,
                        $0014, $0015, $0016, $0017, $0018, $0019, $001A, $001B, $001C, $001D,
                        $001E, $001F, $0020, $0021, $0022, $0023, $0024, $0025, $0026, $0027,
                        $0028, $0029, $002A, $002B, $002C, $002D, $002E, $002F, $0030, $0031,
                        $0032, $0033, $0034, $0035, $0036, $0037, $0038, $0039, $003A, $003B,
                        $003C, $003D, $003E, $003F, $0040, $0041, $0042, $0043, $0044, $0045,
                        $0046, $0047, $0048, $0049, $004A, $004B, $004C, $004D, $004E, $004F,
                        $0050, $0051, $0052, $0053, $0054, $0055, $0056, $0057, $0058, $0059,
                        $005A, $005B, $005C, $005D, $005E, $005F, $0060, $0061, $0062, $0063,
                        $0064, $0065, $0066, $0067, $0068, $0069, $006A, $006B, $006C, $006D,
                        $006E, $006F, $0070, $0071, $0072, $0073, $0074, $0075, $0076, $0077,
                        $0078, $0079, $007A, $007B, $007C, $007D, $007E, $007F,

                        $20AC, $0081, $201A, $0192, $201E, $2026, $2020, $2021, $02C6, $2030,
                        $0160, $2039, $0152, $008D, $017D, $008F, $0090, $2018, $2019, $201C,
                        $201D, $2022, $2013, $2014, $02DC, $2122, $0161, $203A, $0153, $009D,
                        $017E, $0178, $00A0, $00A1, $00A2, $00A3, $00A4, $00A5, $00A6, $00A7,
                        $00A8, $00A9, $00AA, $00AB, $00AC, $00AD, $00AE, $00AF, $00B0, $00B1,
                        $00B2, $00B3, $00B4, $00B5, $00B6, $00B7, $00B8, $00B9, $00BA, $00BB,
                        $00BC, $00BD, $00BE, $00BF, $00C0, $00C1, $00C2, $00C3, $00C4, $00C5,
                        $00C6, $00C7, $00C8, $00C9, $00CA, $00CB, $00CC, $00CD, $00CE, $00CF,
                        $00D0, $00D1, $00D2, $00D3, $00D4, $00D5, $00D6, $00D7, $00D8, $00D9,
                        $00DA, $00DB, $00DC, $00DD, $00DE, $00DF, $00E0, $00E1, $00E2, $00E3,
                        $00E4, $00E5, $00E6, $00E7, $00E8, $00E9, $00EA, $00EB, $00EC, $00ED,
                        $00EE, $00EF, $00F0, $00F1, $00F2, $00F3, $00F4, $00F5, $00F6, $00F7,
                        $00F8, $00F9, $00FA, $00FB, $00FC, $00FD, $00FE, $00FF);



    FUNCTION  Utf8ToAnsi (Source : STRING; UnknownChar : CHAR = '?) : ANSISTRING;
              (* Converts the given UTF-8 String to Windows ANSI (Win-1252).
                 If a character can not be converted, the "UnknownChar" is inserted. *)
    VAR
      SourceLen : INTEGER;  // Length of Source string
      I, K      : INTEGER;
      A         : BYTE;     // Current ANSI character value
      U         : WORD;
      Ch        : CHAR;     // Dest char
      Len       : INTEGER;  // Current real length of "Result" string
    BEGIN
      SourceLen := Length (Source);
      SetLength (Result, SourceLen);   // Enough room to live
      Len := 0;
      I   := 1;
      WHILE I <= SourceLen DO BEGIN
        A := ORD (Source [I]);
        IF A < $80 THEN BEGIN                                               // Range $0000..$007F
          INC (Len);
          Result [Len] := Source [I];
          INC (I);
          END
        ELSE BEGIN                                                          // Determine U, Inc I
          IF (A AND $E0 = $C0) AND (I < SourceLen) THEN BEGIN               // Range $0080..$07FF
            U := (WORD (A AND $1F) SHL 6) OR (ORD (Source [I+1]) AND $3F);
            INC (I, 2);
            END
          ELSE IF (A AND $F0 = $E0) AND (I < SourceLen-1) THEN BEGIN        // Range $0800..$FFFF
            U := (WORD (A AND $0F) SHL 12) OR
                 (WORD (ORD (Source [I+1]) AND $3F) SHL 6) OR
                 (      ORD (Source [I+2]) AND $3F);
            INC (I, 3);
            END
          ELSE BEGIN                                                        // Unknown/unsupported
            INC (I);
            FOR K := 7 DOWNTO 0 DO
              IF A AND (1 SHL K) = 0 THEN BEGIN
                INC (I, (A SHR (K+1))-1);
                BREAK;
                END;
            U := WIN1252_UNICODE [ORD (UnknownChar)];
            END;
          Ch := UnknownChar;                                                // Retrieve ANSI char
          FOR A := $00 TO $FF DO
            IF WIN1252_UNICODE [A] = U THEN BEGIN
              Ch := CHR (A);
              BREAK;
              END;
          INC (Len);
          Result [Len] := Ch;
          END;
        END;
      SetLength (Result, Len);
    END;



  • Profile
    강호수 2002.03.07 21:10
    답변 너무 감사합니다면 변환을 하면 전부 Unknownchar로 나오는데 이거 어떻게 해야 하나요 ?

    지금 MSN에 메시지를 가로체는 것을 하고는 있는데 이게 UTF-8로 되어있거든요


    Content-Type: text/x-msmsgsprofile; charset=UTF-8
    USR 4 OK laker@korea.com ?
    • 강호수
    • 2002.03.07 06:33
    • 3 COMMENTS
    • /
    • 0 LIKES
    • 유성웅
      2002.04.12 01:38
      이건 어떼요 MultiByteToWideChar The MultiByteToWideChar function maps a character string to a ...
    • 이준희
      2002.03.07 18:30
      CONST   // --- Character Translation Table for Unicode <-> Win-1252   WI...
    • 강호수
      2002.03.07 21:10
      답변 너무 감사합니다면 변환을 하면 전부 Unknownchar로 나오는데 이거 어떻게 해야 하나요 ? 지금 MSN...
    • 이수열
    • 2002.03.07 05:31
    • 4 COMMENTS
    • /
    • 0 LIKES
    • 타락천사
      2002.03.07 20:03
      안녕하세여..타락임다...^^ 흐흐, Win2K 를 깔았는데, 잦은 충돌이 있나 보군여.. ^^;; 일부 시스템 ...
    • 이추형
      2002.03.07 18:42
      상위버젼을 사용하고 싶은것은 누구나 다 원하는 일일겁니다. 하지만 개발을 해주어야 하는 입장에서는 ...
    • 권대웅
      2002.03.07 09:25
      안녕하세요! 저도 궁금해서 한마디 적어요..^^;;;; 예전에 98을 사용 했는데.. 다들 아시 겠지만....
    • 그리고
      2002.03.07 06:20
      저는 98 쓰다가 2000으로 바꾼지 오래 됐는데요 ... 아마 개발자들중 아주 많은사람들이 98 안쓰고 2000 ...
    • 김강수
      2002.03.07 06:09
      API함수 없구요. Stored Procedure를 통한 View생성은 가능합니다. 단순 T-SQL문을 쿼리 컴포넌트를 통한...
    • 최군
    • 2002.03.07 03:43
    • 2 COMMENTS
    • /
    • 0 LIKES
    • 권대웅
      2002.03.07 09:30
      저두.. 자료 변환을 위해서... 여기 저기 많이 찾아 다니다가.. 이 컴포넌트를 사용 해 봤습니다. ...
    • 나두초보!!
      2002.03.07 19:10
      자료실에 가보시면.. scExcelExport 컴포런트가 있습니다. 사용하기에두 편하구.. 선두 그려주고..^^;...
    • 이호연
    • 2002.03.07 03:37
    • 2 COMMENTS
    • /
    • 0 LIKES
    • 별볼일없는
      2002.03.07 03:58
      2번 질문은 폼 크래이트를 하지 않아서 인것 같은데요 프리뷰하기 전에 크래이트명령을 한번 해보세요...
    • 이호연
      2002.03.09 08:47
      저도 그랬으면 좋겠습니다. 고수님들 좀 알려주세여 네~~~
    • 권대웅
      2002.03.07 09:40
      writeln(Fprt,'  ', #27+'W1'+'두배출력문자'+#27+'W0'); 예전에 이렇게 해서 출력 한것 같네요...
    • 한수정
    • 2002.03.07 03:14
    • 1 COMMENTS
    • /
    • 0 LIKES
    • 이추형
      2002.03.14 19:59
      XP를 다시 설치하세요 (win 2000은 2000번, win 98은 98번.... 아시겠죠?)
    • 한만교
      2002.03.07 03:57
      Application.MessageBox('화일복사가 완료되었습니다.', '확인', MB_OK);
    • 김윤곤
    • 2002.03.07 02:30
    • 0 COMMENTS
    • /
    • 0 LIKES
    • 김용성
      2002.03.07 03:57
      이렇게 한번 다시 작성해보시죠... 그리고 Database 컴포를  더블 클릭하면 기본 설정 사항이 ...
    • 장지용
      2002.03.07 02:29
      혹시 폴더명에 괄호 "(" 가 들어가 있지 않나요?
    • 깨비
    • 2002.03.07 01:46
    • 4 COMMENTS
    • /
    • 0 LIKES
    • 루단스
      2002.03.07 02:59
      전송하는건 아니고 단지 옆 image컴포에다가 보여주는거를 지금 하고계시는건가요? 그렇다면.. Framet...
    • 깨비
      2002.03.07 04:24
      제가 하려는건.... 제가 쓰는 소켓은. 스트링만을 전송하는듯 합니다. 그래서 lpVHdr^.lpData 데이타를 ...
    • 최용일
      2002.03.07 19:05
      안녕하세요. 최용일입니다. 아래 코드참조하세요...     fbmpFrameImage : TBit...
    • 깨비
      2002.03.07 21:56
      감사드립니다... 꾸우벅..
    • 홍성락
      2002.03.07 03:32
      ////////////////////////////////////////////////////////// 혹시 폼이 자동생성인지 프로젝트 옵션에...
    • 프로초보
      2002.03.07 19:12
    • 권혁규
    • 2002.03.07 00:53
    • 0 COMMENTS
    • /
    • 0 LIKES
    • 홍성락
      2002.03.07 01:45
      /////////////////////////////////////// 아래 간단히 해봤습니다... 관련된 함수를 도움말에서 찾고 ...
    • 델을배우고파
      2002.03.07 02:06
      답변 감사합니다.. 그리고 한가지 더 궁금한게 있는데요.. 이렇게 30분전이나 1시간전의 날자나 시간...
    • 홍성락
      2002.03.07 04:40
      ////////////////////////////////////////////////////////// 테스트 하기엔 좀 시간이 없구요... 혹...
    • 이정석
    • 2002.03.07 00:35
    • 2 COMMENTS
    • /
    • 0 LIKES
    • 김강수
      2002.03.07 06:05
      종이 싸이즈를 Custom 으로 하시고, 가로 세로 길이를 mm나 inch 단위로 정확하게 기재 하세요. 그러면 ...
    • 이정석
      2002.03.07 18:42
      답변 감사합니다. 하지만 커스텀 사이즈로 하고 자로 크기를 재어 마추었는데도 다르게 나오네요. 프린터 ...
    • ssjeon
    • 2002.03.07 00:21
    • 1 COMMENTS
    • /
    • 0 LIKES
    • 박상윤
      2002.03.09 03:51
      메모리를 사용하여 분리해서 로드 하시면 될것 같은데... 그것두 그렇지만  화면에 보여주...
    • 신용탁
    • 2002.03.06 23:04
    • 3 COMMENTS
    • /
    • 0 LIKES
    • 홍성락
      2002.03.06 23:13
      //////////////////////////////////// 간단히 익스프롤러를 외부로 띄우는거로 말씀드리겠습니다. 유...
    • 신용탁
      2002.03.07 00:07
      님... 답변 감사합니다. 근데... 웹브라우저를 외부에 띄울려고 하는게 아니라, 어플리케이션 내부에서 ...
    • 사이비
      2002.03.07 00:09
      WebBrowser1.Navigate('http://www.delphi.co.kr'); 티웹브라우져 콤포넌트 쓰이소.