Showing posts with label 유니코드. Show all posts
Showing posts with label 유니코드. Show all posts

2016/10/04

Write and read unicode test using wfstream

fstream을 이용하여 유니코드 문자를 저장하려 시도하였다. 쉽게 "w"만을 붙여서 wofstream을 이용하고 wchar_t 또는 wstring을 출력만 하면 될 줄 알았으나 기대대로 동작을 하지 않았다. 문자열 인코딩 문제등등 여러가지 복잡한 문제가 있었다. 여러 정보를 취합해 나중에 도움이 될 수 있도록 코드를 남겼다.

class Unicodecvt : public std::codecvt<wchar_t, char, mbstate_t>
{
protected:
 virtual bool do_always_noconv() const
 {
  return true;
 }
};

// Writting
std::wofstream fOut(L"D:\\test.txt", std::ios_base::binary);
fOut.imbue(std::locale(std::locale(""), ::new Unicodecvt));
fOut << wchar_t(0xFEFF);  // write BOM (UTF-16LE)
fOut << L"유니코드 출력 테스트: " << 12345 << std::endl;
fOut.close();

// Reading
std::wifstream fIn(L"D:\\test.txt", std::ios_base::binary);
fIn.imbue(std::locale(std::locale(""), ::new Unicodecvt));
if ( fIn.is_open() )
{
 fIn.seekg(2); // Skip BOM
 while ( !fIn.eof() )
 {
  wchar_t wszBuf[1024] = { 0, };
  fIn.getline( (wchar_t*)wszBuf, _countof(wszBuf));
 }
}
fIn.close();


참조 문서
https://golbenge.wordpress.com/2009/12/24/stl을-이용한-unicode-텍스트-파일-출력 http://saneh.tistory.com/entry/STL-유니코드-저장읽기

2015/08/27

Chrome 한글 이름 파일명 다운로드 할 때 깨지는 문제

Chrome 의 언어 설정이 한글일 경우는 문제가 없어 보이는데 최근 Windows 10을 테스트 하면서 Chrome의 실행 문제로 Canary Build를 사용하는데 이 녀석은 설치시 영문으로 설치 된다. 사용중 한글 이름으로 된 파일을 다운로드 받을 경우 이름이 깨지는 것을 경험하고 이 문제의 해결 방법을 찾아 보니 인코딩 설정을 변경해 주면 해결 된다고 한다.


아래 설정 위치에서 인코딩을 유니코드로 설정해 주면 해결된다.
Settings -> Advanced Settings -> Web content -> Customize fonts... -> Encoding
(한글 UI: 설정 -> 고급 설정 -> 웹 콘텐츠 -> 글꼴 맞춤설정 -> 인코딩


Original Post : http://neodreamer-dev.tistory.com/757

2008/07/11

유니코드와 아스키코드간의 변환 함수

MySQL++ 라이브러리 관련되어 테스트를 해보다 보게된 함수이다.

함수 스택 버퍼를 사용하는 W2A, A2W 보다 간단하게 함수를 만들어 사용하는게 메모리 운영면에서 여러모로 좋다.


//// ToUCS2 ////////////////////////////////////////////////////////////
// Convert a C string in UTF-8 format to UCS-2 format.

bool CExampleDlg::ToUCS2(LPTSTR pcOut, int nOutLen, const char* kpcIn)
{
if (strlen(kpcIn) > 0) {
// Do the conversion normally
return MultiByteToWideChar(CP_UTF8, 0, kpcIn, -1, pcOut,
nOutLen) > 0;
}
else if (nOutLen > 1) {
// Can't distinguish no bytes copied from an error, so handle
// an empty input string as a special case.
_tccpy(pcOut, _T(""));
return true;
}
else {
// Not enough room to do anything!
return false;
}
}


//// ToUTF8 ////////////////////////////////////////////////////////////
// Convert a UCS-2 multibyte string to the UTF-8 format.

bool CExampleDlg::ToUTF8(char* pcOut, int nOutLen, LPCWSTR kpcIn)
{
if (_tcslen(kpcIn) > 0) {
// Do the conversion normally
return WideCharToMultiByte(CP_UTF8, 0, kpcIn, -1, pcOut,
nOutLen, 0, 0) > 0;
}
else if (nOutLen > 0) {
// Can't distinguish no bytes copied from an error, so handle
// an empty input string as a special case.
*pcOut = '\0';
return true;
}
else {
// Not enough room to do anything!
return false;
}
}


Original Post : http://neodreamer-dev.tistory.com/122