글쓴이 한글이름에 이가 또는 가 붙이기
글쓴이: babbab / 작성시간: 토, 2018/09/22 - 9:59오전
한글이름에도 항상 '이' 붙이신거 보이시죠?
이것을 향상하는 간단한 루틴을 올려보겠습니다.
이름이 한글이라면 가 아니면 이가 를 발음에 따라 붙입니다.
<code> #include <stdio.h> #include <wchar.h> #include <locale.h> int main() { setlocale(LC_ALL, "korean"); wchar_t *str1 = L"막내"; wchar_t *str2 = L"갑순"; char cho, joong, jong; cho = (str1[1] - 0xAC00) / (21*28); joong = (str1[1] - 0xAC00) % (21 * 28) / 28; jong = (str1[1] - 0xAC00) % 28; wprintf(L"%ls%ls\n", str1, (jong != 0) ? L"이가" : L"가") ; cho = (str2[1] - 0xAC00) / (21*28); joong = (str2[1] - 0xAC00) % (21 * 28) / 28; jong = (str2[1] - 0xAC00) % 28; wprintf(L"%ls%ls\n", str2, (jong != 0) ? L"이가" : L"가") ; } 결과 막내가 갑순이가 </code>
윈도우 mingw 에서 컴파일 했습니다.
리눅스에서 LC_ALL, ""을 쓰셔야 할겁니다.
Forums:


codepad 웹 컴파일러에서 되도록 변경해봤습니다.
http://codepad.org/BfhbJzmJ
#include <stdio.h> #include <stdlib.h> #include <wchar.h> #include <locale.h> #define DF_C_PLUS 1 #if DF_C_PLUS #include <iostream> #include <string> #include <iostream> #include <string> #include <sstream> //#include <codecvt> #endif //Error: ‘Getch’ Was Not Declared In This Scope //https://www.dreamincode.net/forums/topic/341445-error-getch-was-not-declared-in-this-scope/ //htons function //https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-htons //WinSock32 //https://social.msdn.microsoft.com/Forums/vstudio/en-US/82ee2e1b-9957-4147-ac31-4e9ac82cbd59/winsock32?forum=vcgeneral // /workspace/test/src/client.c:76: error: ‘_getch’ was not declared in this scope #if DF_C_PLUS using namespace std; #endif #if 0 //[WinAPi]WinAPI를 이용하여 cp949(ANSI) - utf8 인코딩 변환 C/C++ //http://blog.naver.com/PostView.nhn?blogId=nimi315&logNo=50093464158 //UTF-8 을 ANSI 로 변환 (한글 출력에 사용) string utf8_to_ansi(string &utf8) { wchar_t unicode[1500]; char ansi[1500]; memset(unicode, 0, sizeof(unicode)); memset(ansi, 0, sizeof(ansi)); // ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, unicode, sizeof(unicode)); // ::WideCharToMultiByte(CP_ACP, 0, unicode, -1, ansi, sizeof(ansi), NULL, NULL); return string(ansi); } unicode to ansi c++ UNICODE, ANSI 인코딩 변환 http://pyoungon.tistory.com/30 [Pyoungon's Blog] STEP 0: 로케일 설정 (시스템의 기본 언어로 설정) setlocale( LC_ALL, "" ); //STEP 1: 문자 변환 STEP 1-1 ANSI ==> UNICODE mbtowc( UNICODE 문자, ANSI 문자 ); STEP 1-2: UNICODE ==> ANSI wctomb( ANSI 문자, UNICODE 문자 ); //STEP 2: 문자열 변환 STEP 2-1: ANSI ==> UNICODE mbstowcs( UNICODE 문자열, ANSI 문자열, UNICODE 문자열 최대길이 ); STEP 2-2: UNICODE ==> ANSI wcstombs( ANSI 문자열, UNICODE 문자열, ANSI 문자열 최대길이 ); How to convert a UNICODE string to ANSI http://www.cplusplus.com/forum/general/192417/ wcstombs 함수 http://ehpub.co.kr/wcstombs-%ED%95%A8%EC%88%98/ Reload this Page wstring <-> utf8 conversion in pure C++ https://www.linuxquestions.org/questions/programming-9/wstring-utf8-conversion-in-pure-c-701084/ std::codecvt_utf8로 wstring을 utf-8 문자열로 변환 하기 https://jacking75.github.io/std_codecvt_utf8/ C++11 - Convert to/from UTF-8/wchar_t https://ryanclouser.com/2016/08/11/C-11-Convert-to-from-UTF-8-wchar-t/ std::codecvt_utf8 https://en.cppreference.com/w/cpp/locale/codecvt_utf8 wstring_convert wchar_t to utf8 #endif typedef std::string Str; typedef std::wstring WStr; void utf8toWStr(WStr& dest, const Str& src){ dest.clear(); wchar_t w = 0; int bytes = 0; wchar_t err = L'�'; for (size_t i = 0; i < src.size(); i++){ unsigned char c = (unsigned char)src[i]; if (c <= 0x7f){//first byte if (bytes){ dest.push_back(err); bytes = 0; } dest.push_back((wchar_t)c); } else if (c <= 0xbf){//second/third/etc byte if (bytes){ w = ((w << 6)|(c & 0x3f)); bytes--; if (bytes == 0) dest.push_back(w); } else dest.push_back(err); } else if (c <= 0xdf){//2byte sequence start bytes = 1; w = c & 0x1f; } else if (c <= 0xef){//3byte sequence start bytes = 2; w = c & 0x0f; } else if (c <= 0xf7){//3byte sequence start bytes = 3; w = c & 0x07; } else{ dest.push_back(err); bytes = 0; } } if (bytes) dest.push_back(err); } void wstrToUtf8(Str& dest, const WStr& src){ dest.clear(); for (size_t i = 0; i < src.size(); i++){ wchar_t w = src[i]; if (w <= 0x7f) dest.push_back((char)w); else if (w <= 0x7ff){ dest.push_back(0xc0 | ((w >> 6)& 0x1f)); dest.push_back(0x80| (w & 0x3f)); } else if (w <= 0xffff){ dest.push_back(0xe0 | ((w >> 12)& 0x0f)); dest.push_back(0x80| ((w >> 6) & 0x3f)); dest.push_back(0x80| (w & 0x3f)); } else if (w <= 0x10ffff){ dest.push_back(0xf0 | ((w >> 18)& 0x07)); dest.push_back(0x80| ((w >> 12) & 0x3f)); dest.push_back(0x80| ((w >> 6) & 0x3f)); dest.push_back(0x80| (w & 0x3f)); } else dest.push_back('?'); } } Str wstrToUtf8(const WStr& str){ Str result; wstrToUtf8(result, str); return result; } WStr utf8toWStr(const Str& str){ WStr result; utf8toWStr(result, str); return result; } std::ostream& operator<<(std::ostream& f, const WStr& s){ Str s1; wstrToUtf8(s1, s); f << s1; return f; } std::istream& operator>>(std::istream& f, WStr& s){ Str s1; f >> s1; utf8toWStr(s, s1); return f; } bool utf8test(){ WStr w1; //for (wchar_t c = 1; c <= 0x10ffff; c++){ for (wchar_t c = 0x100000; c <= 0x100002; c++){ w1 += c; } Str s = wstrToUtf8(w1); WStr w2 = utf8toWStr(s); bool result = true; if (w1.length() != w2.length()){ printf("length differs\n"); //std::cout << "length differs" << std::endl; result = false; } printf("w1: %ls\ns: %s\nw2: %ls\n", w1.c_str(), s.c_str(), w2.c_str()); for (size_t i = 0; i < w1.size(); i++) if (w1[i] != w2[i]){ result = false; printf("character at pos %x differs (expected %.8x got %.8x)\n", i, w1[i], w2[i]); //std::cout << "character at pos " << i << " differs" << std::endl; break; } if (!result){ printf("utf8 dump: \n"); for (size_t i = 0; i < s.size(); i++) printf("%2x ", (unsigned char)s[i]); } return result; } int main(int argc, char** argv) { //------------------------ // //------------------------ std::wstring ws(L"фыва"); std::string s("фыва"); std::cout << ws << s << std::endl; std::cout << wstrToUtf8(utf8toWStr("фыва")) << std::endl; if (utf8test()) std::cout << "utf8Test succesful" << std::endl; else std::cout << "utf8Test failed" << std::endl; //------------------------ // //------------------------ std::wstring str1(L"막내"); std::wstring str2(L"갑순"); char cho, joong, jong; cho = (str1[1] - 0xAC00) / (21*28); joong = (str1[1] - 0xAC00) % (21 * 28) / 28; jong = (str1[1] - 0xAC00) % 28; std::cout << str1; // std::cout << wstrToUtf8((str1.c_str())) << std::endl; if(jong != 0) { printf("이가\n") ; } else { printf("가\n") ; } cho = (str2[1] - 0xAC00) / (21*28); joong = (str2[1] - 0xAC00) % (21 * 28) / 28; jong = (str2[1] - 0xAC00) % 28; std::cout << str2; // std::cout << wstrToUtf8((str1.c_str())) << std::endl; if(jong != 0) { printf("이가\n") ; } else { printf("가\n") ; } return 0; } #if 0 // фывафыва фыва w1: utf8Test succesful 막내가 갑순이가 #endif----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
코드는 간결할수록 이점이 많습니다.
코드는 간결해야 합니다. 같은일을 하는 코드라도 긴게 낫습니까 짧은게 낫습니까?
후자의 코드가 길어진 거는 문자 인코딩 관련된 부분이
후자의 코드가 길어진 거는 문자 인코딩 관련된 부분이 들어가서 더 길어 보이는 거네요. 핵심 논리는 둘 다 똑같습니다.
댓글 달기