심지현의 ExaLogs는 Apache-CXF, SOAPUI, Apache-Tomcat, WebLogic, Hadoop, HBase, Zookeeper, Redis, MongoDB, PostgreSQL, Oracle, MySQL ,Spring, XMPP, C++, JAVA 등의 내용을 다룹니다.
2010년 1월 19일 화요일
UTF-8 to ANSI Char* 변환
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "windows.h"
static wchar_t *make_unicode_string(const unsigned char *utf8)
{
int size = 0, index = 0, out_index = 0;
wchar_t *out;
unsigned char c;
/* first calculate the size of the target string */
c = utf8[index++];
while(c) {
if((c & 0x80) == 0) {
index += 0;
} else if((c & 0xe0) == 0xe0) {
index += 2;
} else {
index += 1;
}
size += 1;
c = utf8[index++];
}
out = (wchar_t*)malloc((size + 1) * sizeof(wchar_t));
if (out == NULL)
return NULL;
index = 0;
c = utf8[index++];
while(c)
{
if((c & 0x80) == 0) {
out[out_index++] = c;
} else if((c & 0xe0) == 0xe0) {
out[out_index] = (c & 0x1F) << 12;
c = utf8[index++];
out[out_index] |= (c & 0x3F) << 6;
c = utf8[index++];
out[out_index++] |= (c & 0x3F);
} else {
out[out_index] = (c & 0x3F) << 6;
c = utf8[index++];
out[out_index++] |= (c & 0x3F);
}
c = utf8[index++];
}
out[out_index] = 0;
return out;
}
int utf8_decode(const char *from, char **to)
{
wchar_t *unicode;
int chars, err;
/* On NT-based windows systems, we could use MultiByteToWideChar(CP_UTF8), but
* MS doesn't actually have a consistent API across win32.
*/
unicode = make_unicode_string((const unsigned char*)from);
if(unicode == NULL)
{
fprintf(stderr, "Out of memory processing string from UTF8 to UNICODE16\n");
return -1;
}
chars = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
-1, NULL, 0, NULL, NULL);
if(chars == 0)
{
fprintf(stderr, "Unicode translation error %d\n", GetLastError());
free(unicode);
return -1;
}
*to = (char*)calloc(chars + 1, sizeof(unsigned char));
if(*to == NULL)
{
fprintf(stderr, "Out of memory processing string to local charset\n");
free(unicode);
return -1;
}
err = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
-1, *to, chars, NULL, NULL);
if(err != chars)
{
fprintf(stderr, "Unicode translation error %d\n", GetLastError());
free(unicode);
free(*to);
*to = NULL;
return -1;
}
free(unicode);
return 0;
}
char* UTF8ToANSI(char *pszCode)
{
BSTR bstrWide;
char* pszAnsi;
int nLength;
// Get nLength of the Wide Char buffer
nLength = MultiByteToWideChar(CP_UTF8, 0, pszCode, lstrlen(pszCode) + 1,
NULL, NULL);
bstrWide = SysAllocStringLen(NULL, nLength);
// Change UTF-8 to Unicode (UTF-16)
MultiByteToWideChar(CP_UTF8, 0, pszCode, lstrlen(pszCode) + 1, bstrWide,
nLength);
// Get nLength of the multi byte buffer
nLength = WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, NULL, 0, NULL, NULL);
pszAnsi = new char[nLength];
// Change from unicode to mult byte
WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, pszAnsi, nLength, NULL, NULL);
SysFreeString(bstrWide);
return pszAnsi;
}
main()
{
FILE *fp;
char str[80];
char *str2;
fp=fopen("data.txt","r");
fscanf(fp,"%[^\n]",str);
// utf8_decode(str,&str2);
str2=UTF8ToANSI(str);
printf("%s\n",str2);
getch();
}
Link with oleaut32.lib.
[출처] UTF8 --> char* 변환|작성자 난뽀다
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기