UTF-8 컨버젼 매크로(UTF-8 String Conversion Macros)

Software Development/Article 2008/06/26 18:10

Introdution

UTF-8 string을 처리하는 경우 UTF-8 string <-> Ansi string 또는 UTF-8 string <-> Unicode string 간의 변환이 종종 필요할 때가 있다. MultiByteToWideChar 함수와 WideCharToMultiByte를 이용하면 변환이 가능하지만 이 작업을 꽤나 까다롭게 느끼시는 분들이 많이 있는 것 같다. 특히 UTF-8 string <-> Ansi string 의 경우 두 단계를 거쳐서 변환해야하기 때문에 이 부분에 대해서 질문 하시는 분이 많았다. 이런 점을 조금이나마 개선해 보고자 한 문장으로 변환할 수 있는 매크로를 만들게 되었으며, 이 글에서는 이 매크로에 대해서 설명하고자 한다.

How To

인터페이스는 Unicode string <-> Ansi string 변환 시에 많이 사용하는 ATL String Conversion Macros를 흉내내서 만들어 봤다. 사용해본 사람은 알겟지만 Conversion Macros가 MultiByteToWideChar등의 함수를 직접 사용하는 것보다 훨씬 쉽고 간편하다. UTF-8 String Conversion Macros 에서는 두 가지 방식의 매크로를 제공하는데 한 가지는 스택 메모리를 사용하는 방식(ex. A2UTF8)이고 또 한가지는 전자보다는 좀 더 안전하게 힙 메모리를 사용하는 방식(ex. A2UTF8_EX)이다. 물론 사용자가 구현 방식을 알고 있을 필요는 없다. 다만 엄청나게 긴(스택에 담을 수 없을 만큼) 문자열을 다룰 경우 후자가 더 안전하다는 것만 알고 있으면 된다.

Usage

ATL String Conversion Macros의 용법을 알고 있다면 UTF-8 String Conversion Macros도 사용하기 매우 쉬울것이다. 하지만 모른다 해도 어려운 것은 아니니 걱정할 필요는 없다. 용법을 살펴보면 다음과 같다.

SourceType2DestinationType[EX]

SourceType과 DestinationType에는 아래에서 설명할 타입을 넣어주면 되고 힙을 이용한 방식 사용할 경우 뒤에 _EX를 붙여주기만 하면 된다. 변환 가능한 타입은 아래와 같다.

A               ANSI character string.
W              Unicode character string.
T               Generic character string.
UTF8          UTF-8 encoded character string.

ATL String Conversion Macros와 다른 점은 UTF8이 추가된 것 뿐이고, 당연한 얘기지만 SourceType 과 DestinationType중 하나는 반드시 UTF8이어야 한다. 예를 들어 ANSI string -> UTF-8 string 의 변환의 경우 A2UTF8 또는 A2UTF8_EX를 사용하면 된다.

마지막으로 한가지 알아야 할 것은 매크로를 사용하기 전에 UTF8_CONVERSION / UTF8_CONVERSION_EX 매크로를 선언해 줘야 한다는 것이다. 이것 또한 ATL String Conversion Macros과 같으니 쉽게 이해할 것이라 생각하고, 간단한 예제로 설명을 마치도록 하겠다.



Copyright (C) 2008 by KiloJuliet
http://www.kilojuliet.com
kilojuliet@paran.com
크리에이티브 커먼즈 라이센스
Creative Commons License
Trackback 0 : Comments 17

Trackback Address :: http://kilojuliet.tistory.com/trackback/36 관련글 쓰기

  1. 김진홍 2008/09/09 19:43 Modify/Delete Reply

    데브피아에서 다음글 보고 이렇게 연락드립니다

    일단 RFID 리더와 PC와의 통신 부분(TCP아니면 시리얼이겠져?)이 있겠고
    나머지는 회원 등록, 관리등일텐데요..
    RFID면 프로토콜도 복잡하진 않을거 같고, 통신하는데 큰 어려움은 없을것 같은데
    회원 관리라는것은 요구사항에 따라서 단순할수도 복잡할수도 있겠네요..
    일단 RFID와 통신하는 테스트 모듈을 작성해보시고 회원관리에 대한 부분은
    다른 관리프로그램을 많이 참고해 보시는게 좋겠어요

    제가 지금 RFID로 태그를 읽는것 까지는 성공했는데
    통신때문에 골머리를 썩고 있습니다
    C++로 테그를 읽었지만 이것을 컴퓨터에서 UART를 통해 AVR로 보내려고 하는데
    이 소스에서 막막해졌습니다
    통신에 대한 공부가 부족해서요...
    혹시 이 부분에 대한 조언을 얻을수 없을지 궁금해서요
    혹시 예제 소스 이런거 없나요??

    • kilojuliet 2008/09/22 09:38 Modify/Delete

      UART라면 시리얼 통신입니다.

      시리얼 통신은 CreateFile로 시리얼 포트를 열고 파일에 읽고 쓰듯이 사용하면 되니 어렵지 않을겁니다.

      예제도 많이 있으니 데브피아나 코드프로젝트 등에서 검색해보시길..

  2. madkoala 2009/03/15 00:51 Modify/Delete Reply

    딱 찾고 있던 거였는데, 굉장히 편리하고 유용하네요.
    감사하게 잘 쓰겠습니다 ^^

  3. YangRobin34 2011/11/22 08:50 Modify/Delete Reply

    I took my first <a href="http://goodfinance-blog.com/topics/mortgage-loans">mortgage loans</a> when I was 20 and that supported my relatives a lot. Nevertheless, I need the small business loan again.

  4. Pay Essays 2011/12/05 09:12 Modify/Delete Reply

    How should I start my term papers? Is it worth to use Buying Essay Papers services? Probably I should strive to create essay papers myself?

  5. buy writing paper 2011/12/05 09:12 Modify/Delete Reply

    I realize that real professionals who are employed at custom writings company are able to get a right way from any complicated position. Furthermore, people have to respect their contribution simply because they try hard to fulfill our academic papers writing needs!

  6. article submission service 2011/12/30 00:18 Modify/Delete Reply

    We added the good theme relating with this post taking the help of the article submission services or some submit article service.

  7. essay writing service 2011/12/31 11:06 Modify/Delete Reply

    I would like to tell that I have have got good experience of analysis essays ordering. I claim that the essay writing service would be a good assistant in the research papers performing case.

  8. essay writing service 2012/01/01 00:59 Modify/Delete Reply

    My only goal at this site is to see fellows who work at the great essay writing service. I am very tired of writing custom papers permanently!

  9. thesis service 2012/01/09 05:10 Modify/Delete Reply

    Thesis writing service or dissertation would be acceptable for students. And you accomplish superior french dissertation just like this post.

  10. custom writing 2012/01/09 05:24 Modify/Delete Reply

    The essay writing about this good post, students should find at the custom writing service. Purchase the pre written essay or custom essays about this topic.

  11. Thesis Writing Services 2012/02/15 20:57 Modify/Delete Reply

    I think this is best post I've ever read .. amazing wonderful

  12. forum posting services 2012/03/23 12:09 Modify/Delete Reply

    Frequently that seems that internet is filled by web blogs and you are able to seek for a blog of any topic. Those quantity of blogs can make back links extra efficient. The forum posting service can determine blogs that fit your topic. Hence, you can get right customers and higher page rank!

  13. dissertation sample 2012/04/18 16:21 Modify/Delete Reply

    very simply explained. It is indeed an art to read & stop new visitors with your attractive writing style. I am really impress from your posted information. Thanks for sharing.

  14. Management Thesis Proposal 2012/04/23 23:03 Modify/Delete Reply

    very nice blog and I really appreciate your hard work .. and I hope you update your blog daily

  15. APA Style Research Essay 2012/04/28 02:37 Modify/Delete Reply

    Cool little post! Thanks for taking the time to compile this. I also recognized more than 50% of ideas: If learning this puzzle not an easy task to battle with, I would buy Reflective Essay and get a top class custom written paper performed by writing service writers. Great taste can't be denied!!!

  16. custom coursework 2012/05/13 12:27 Modify/Delete Reply

    Require high quality course essays but don't understand where to buy custom coursework? Utilize the best course works writing corporations! That is not very hard to find some of them in the internet.

Write a comment

◀ PREV : [1] : [2] : [3] : [4] : [5] : ... [36] : NEXT ▶