C 에서 한글 처리

매그넘의 이미지

안녕하세요...

새내기..직장인인데.. 그동안 너무나 궁금했던 점이 있었는데.. 조언좀 부탁드리겠습니다.

int main(void)
{
char str[] = "안녕하세요";
printf("%c\n",str[0]); //이상한 값 출력
printf("%d\n", str[0]); // -66 출력

exit(0);
}

char 배열에 한글을 넣었을때.. 문자열을 통째로 출력 (%s 지시자) 하면.. 아주 정상적으로 출력이 잘되는데.

배열의 한개의 요소 (char) 를 출력할려고 하면.. 이상한값이 나오고...

문자라는 실상은 code 로 이루어져 있어서 %d 로 숫자로 출력을 하면 -66 이란 값이 나오는데...( 'a' 를 출력하면 ascii code 97 이 나오는것처럼..생각을 했습니다~)

char 배열에서 한글이 어떤식으로 처리가 되고. 돌아가는지..

그리고 문자열 "안녕하세요" 가 있을때 "안" 만 출력을 하려면 어찌 해야하는지..

조언 좀 부탁드리겠습니다.

혹시나.. 이미 많이 올라온 내용일경우라면..(검색을 해봤는데..만족할 만한 내용은 못찾았습니다.. ㅡㅡ;) 검색어 를 추천해주세요!!!

sonchac의 이미지

%c%c 하시고 str[0], str[1] 해서 이어서 출력하시면 될 듯하네요.

예를 들면
printf("%c%c", str[0], str[1]);
이렇게요. ^^

keedi의 이미지

utf8 환경이시라면 3바이트 출력을...

---------------------------
Smashing Watermelons~!!
Whatever Nevermind~!!

----
use perl;

Keedi Kim

죠커의 이미지

대부분의 환경에서 char는 옥텟(8비트)이고 char의 배열의 한 요소 역시 옥텟입니다. 한글은 옥텟으로 표현이 불가능합니다.

- CN의 낙서장 / HanIRC:#CN

esrevinu의 이미지

wchar_t type에 대한 이야기나 인코딩에 대한 이야기가 나오네요. multibyte 문자에 대해서는 wchar_t type을 써야 되지 않을까요?

Quote:

UNICODE UNDER LINUX
Under GNU/Linux, the C type wchar_t is a signed 32-bit integer type.
Its values are always interpreted by the C library as UCS code values
(in all locales), a convention that is signaled by the GNU C library to
applications by defining the constant __STDC_ISO_10646__ as specified
in the ISO C 99 standard.

UCS/Unicode can be used just like ASCII in input/output streams, termi?
nal communication, plaintext files, filenames, and environment vari?
ables in the ASCII compatible UTF-8 multi-byte encoding. To signal the
use of UTF-8 as the character encoding to all applications, a suitable
locale has to be selected via environment variables (e.g.,
"LANG=en_GB.UTF-8").

The nl_langinfo(CODESET) function returns the name of the selected
encoding. Library functions such as wctomb(3) and mbsrtowcs(3) can be
used to transform the internal wchar_t characters and strings into the
system character encoding and back and wcwidth(3) tells, how many posi?
tions (0?2) the cursor is advanced by the output of a character.

Under Linux, in general only the BMP at implementation level 1 should
be used at the moment. Up to two combining characters per base charac?
ter for certain scripts (in particular Thai) are also supported by some
UTF-8 terminal emulators and ISO 10646 fonts (level 2), but in general
precomposed characters should be preferred where available (Unicode
calls this Normalization Form C).


--
foldl (flip (:)) [] "universe"
막코더의 이미지

저도 정말 궁금한게 있는데여..
ascii니 utf니 하는 코드들은 어느 시점에서 적용 되는거죠??
코드들을 어디서 관리를 하고 있다고..프로그램 실행시 어느 시점에서 코드가 적용 되는지 좀 갈켜 주세염..;;;;

pok의 이미지

아마 소스의 인코딩에 따라 소스에 쓰여진 문자열이라든지 문자열상수등이 결정되는것으로 보입니다. 소스 인코딩을 euc-kr로 해서 컴파일을 하면 소스내에서 입력한 문자값들은 euc-kr로 인코딩 되어 있을것이고, 그 인코딩을 읽을수 있는 환경에서만 읽을수 있겠죠...


poklog at http://poksion.cafe24.com/poklog/

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.