유니코드 변환 (예) '가'->'0xAC00'

hoollahoop의 이미지

제목처럼 하려면 어떻게 해야 하나요..

변환하려는 문자열은 예처럼 한글뿐만 아니라

일본어 문자셋, 중국어 문자셋 등이 모두 올 수 있어야 합니다.

그리고 역으로 '0xAC00'->'가' 처럼도 되게 하려고 합니다.

임의로 제가 매핑 테이블을 만들어야 하나요?

maptable[][] = { {'가', '0xAC00'} , ... } 이런식으로요?
누가 만들어 놓은게 있나요?

제가 문자셋 관련해서 지식이 거의 없다보니..

질문도 맞게 드린건지 모르겠네요..

꼭 답변 부탁드립니다.

File attachments: 
첨부파일 크기
Image icon result.GIF56.61 KB
송효진의 이미지

iconv 로 UCS-2 로 변환하시고,
bin2hex (ord) 하시고 앞에 0x 붙이세요.

반대로도 쉽겠죠.

송효진의 이미지

원본이 UTF-8 이라면 utf8 to ucs2 로 구글링하면 계산만으로 변환되는 함수가 나옵니다.

익명 사용자의 이미지

응용해 보세요.
libiconv를 사용한 예제입니다.

#include <stdio.h>
#include <iconv.h>

main()
{
   char buf[] = "가";
   char obuf[1024];
   iconv_t cd;
   char *ip, *op;
   int ileft, oleft;

   cd = iconv_open("utf-16be", "utf-8");
   ip = buf;
   op = obuf;
   oleft = 1024;
   ileft = strlen(buf);
   iconv(cd, &ip, &ileft, &op, &oleft);
   printf("'%s'( utf-8 )==> '%s'(utf-16be)\n", buf, obuf);
   printf("utf-16be code : 0x%02X, 0x%02X\n", (unsigned char)*(obuf), (unsigned char)*(obuf+1));
   printf("=================\n");
   printf("{'%s', '0x%02X%02X'}\n", buf, (unsigned char)*(obuf), (unsigned char)*(obuf+1));
   iconv_close(cd);
   return 0;
}
위 소스를 test.c로 저장하고,
$ gcc -o test test.c
Quote:
$ ./test
'가'(utf-8 )==> '▒'(utf-16be)
utf-16be code : 0xAC, 0x00
=================
{'가', '0xAC00'}

* 주의

Quote:
$ locale
LANG=ko_KR.UTF-8
LC_CTYPE="ko_KR.UTF-8"
LC_NUMERIC="ko_KR.UTF-8"
LC_TIME="ko_KR.UTF-8"
LC_COLLATE="ko_KR.UTF-8"
LC_MONETARY="ko_KR.UTF-8"
LC_MESSAGES="ko_KR.UTF-8"
LC_PAPER="ko_KR.UTF-8"
LC_NAME="ko_KR.UTF-8"
LC_ADDRESS="ko_KR.UTF-8"
LC_TELEPHONE="ko_KR.UTF-8"
LC_MEASUREMENT="ko_KR.UTF-8"
LC_IDENTIFICATION="ko_KR.UTF-8"
LC_ALL=

했을때
제 경우에는, utf-8이 터미널 세팅입니다.

만일, 다른 터미널(예를들어 EUC-KR)이라면, iconv_open("utf-16BE", "EUC-KR") ;로 소스를 고쳐서 돌리셔야 한다는...

cococo의 이미지

EUC 형태 코드를 Unicode로 바꾸는 방법을 찾아보다가, 이 글을 찾게 되어, 코드대로 해 봤습니다.

되더군요. ( 코드 올려 주신 분 감사드립니다. :) )

근데, 이걸 함수로 만들어 보니, 예상 이외의 값이 나와 , 다시 올려봅니다.

#include <stdio.h>
#include <iconv.h>

int EUCtoUTF16(char **source, char **container,  int sourceLength, int containerLength)
{
	// change from EUC(buf) to  UTF16(buf)
	// source: EUC-JP form's data buffer
	// container: UTF16 form.
	// sourceLength : byte size of string(1byte = length);
	// containerLength : byte size of string(1byte = length);

	iconv_t cd;
	int i,l = sourceLength;

	if (sourceLength > containerLength) 
	{	printf(" \n EUCtoUTF16;parameter(%d. %d.) error.", sourceLength, containerLength);
		return -1;
	}
	cd = iconv_open("utf-16be", "EUC-JP");
	iconv( cd, source, &sourceLength, container, &containerLength );

	printf(" \n result:%s(euc)->%s(utf)\n",source[0], container[0] );
	for( i = 0; i < l; i+=2)
		printf("%x.%x->%x.%x.\n", (source[0])[i], 
	(source[0])[i+1], (container[0])[i], (container[0])[i+1] );

	iconv_close(cd);

	return 1;
};


int main()
{
	char buf[] = "、ォスメ、ッ";
	char obuf[1024];

	iconv_t cd;
	char *ip, *op;
	int ileft, oleft;
	
	ip = buf;
	op = obuf;
	oleft = 1024;
	ileft = strlen(buf);
	
	EUCtoUTF16( &ip, &op, 8 , 8);
	printf(" \n result:%s(EUC-JP)->%s(utf-16)", buf, obuf);
	printf(" \n code:(UTF-16):%02x.%02x", obuf[0],obuf[1]);
	printf(" \n result:%s(EUC-JP)->%s(utf-16)", buf+2, obuf+2);
	printf(" \n code:(UTF-16):%x.%x", obuf[2],obuf[3]);
	printf(" \n result:%s(EUC-JP)->%s(utf-16)", buf+4, obuf+4);
	printf(" \n code:(UTF-16):%x.%x", obuf[4],obuf[5]);
	printf(" \n result:%s(EUC-JP)->%s(utf-16)", buf+6, obuf+6);
	printf(" \n code:(UTF-16):%x.%x", obuf[6],obuf[7]);
	
	return 1;
}

결과 그림을 보면 아시겠지만, 변환 자체는 제대로 되는 데도 불구하고, 함수 안에서의 source, container 의 코드 값은 이상하네요.

특히 이해가 안 되는게, unsigned char임에도 %x로 값 찍어보면, 4바이트짜리 나옵니다. -_-;

부디... 제게 깨달음을...

댓글 첨부 파일: 
첨부파일 크기
Image icon 0바이트
kewlbear의 이미지

ckbcorp wrote:
특히 이해가 안 되는게, unsigned char임에도 %x로 값 찍어보면, 4바이트짜리 나옵니다. -_-;

부디... 제게 깨달음을...

%x는 unsigned int를 출력할 때 쓰는 것입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.