zoukankan      html  css  js  c++  java
  • [C/C++]_[Unicode转Utf8,Ansi转Unicode,Ansi文件转Utf8文件]

    http://blog.csdn.net/infoworld/article/details/15337665

    场景:

    1.也就只有windows需要那么麻烦,还搞一个ANSI编码。学学mac os x,只需要一个utf8就行。

    2.有时候需要把ansi文件内容转换为utf8编码,读取一行之后,把ansi字符串转换为utf8,之后写入文件。


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #include <assert.h>
    
    char* Unicode2Utf8(const char* unicode)
    {
    	int len;
    	len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL);
    	char *szUtf8 = (char*)malloc(len + 1);
    	memset(szUtf8, 0, len + 1);
    	WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL,NULL);
    	return szUtf8;
    }
    
    char* Ansi2Unicode(const char* str)
    {
    	int dwUnicodeLen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
    	if(!dwUnicodeLen)
    	{
    		return strdup(str);
    	}
    	size_t num = dwUnicodeLen*sizeof(wchar_t);
    	wchar_t *pwText = (wchar_t*)malloc(num);
    	memset(pwText,0,num);
    	MultiByteToWideChar(CP_ACP,0,str,-1,pwText,dwUnicodeLen);
    	return (char*)pwText;
    }
    
    char* ConvertAnsiToUtf8(const char* str)
    {
    	char* unicode = Ansi2Unicode(str); 
    	char* utf8 = Unicode2Utf8(unicode);
    	free(unicode);
    	return utf8;
    }
    
    int main(int argc, char *argv[])
    {
    	printf("Hello, world
    ");
    	//1.构造一个ansi文件,内容是"中文abc",看hex编码.
    	//ansi: D6 D0 CE C4 61 62 63
    	//utf8: E4 B8 AD E6 96 87 61 62  63
    	char ansi[] = {0xD6,0xD0,0xCE,0xC4,0x61,0x62,0x63,0};
    	char utf8[] = {0xE4,0xB8,0xAD,0xE6,0x96,0x87,0x61,0x62,0x63,0};
    	char* str = ConvertAnsiToUtf8(ansi);
    	assert(!strcmp(str,utf8));
    	free(str);
    	return 0;
    }
    


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    字典_序列解包用于列表元组字典
    字典_序列解包用于列表元组字典
    字典_元素的访问_键的访问_值的访问_键值对的访问
    字典_特点_4种创建方式_普通-dict
    元组_生成器推导式创建元组-总结
    MySql高级
    技术点
    全文检索ElasticSearch
    数仓管理
    SpringCache
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834776.html
Copyright © 2011-2022 走看看