user-0 提问: utf-8 和 GB2312 汉字编码方式问题

回复:
user: user-0
content: 问题已解决，Linux下采用iconv函数  *************************main.c***************************** #include <stdio.h> #include <stdlib.h> #include <string.h>  #include "iconv_app.h" #include "file.h"  #define OUTLEN 255 int main(void) { 	int rc; 	char *in_utf8 = "靠靠?;//"姝ｅ?ㄥ??瑁?"; 	char *in_gb2312 = "正在安装"; 	char out[OUTLEN];  while(1) { file_open();  //unicode码转为gb2312码 rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN); printf("unicode-->gb2312 out=%s\n",out); file_write_string(out);  //gb2312码转为unicode码 rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN); printf("gb2312-->unicode out=%s\n",out); file_write_string(out);  file_close(); sleep(10); } 	  return 0; }   ******************iconv_app.c*************************** #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iconv.h>   //代码转换:从一种编码转为另一种编码 int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen) { iconv_t cd; int rc; char **pin = &inbuf; char **pout = &outbuf;  cd = iconv_open(to_charset,from_charset); if (cd==0) return -1; memset(outbuf,0,outlen); if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1; iconv_close(cd); return 0; } //UNICODE码转为GB2312码 int u2g(char *inbuf,int inlen,char *outbuf,int outlen) { return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen); } //GB2312码转为UNICODE码 int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen) { return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen); }

user: user-1
content: 如果你用的是linux g++，那么就要用iconv库，转换下就可以了

user: user-2
content: //TXT文件用于表示UTF8编码的文件头 const unsigned char szHead[3] = {0xEF, 0XBB, 0XBF}; FILE *pFile = fopen("d:\\2.txt", "w"); fwrite(szHead, sizeof(char), 3, pFile); //fprintf(所在位置:经度%f,纬度%f,地址:%s); //.... fclose(pFile); pFile = NULL;   创建TXT文件的时候写入0XEF,0XBB,0XBF表示这个TXT文件是以UTF8进行编码的，这样打开TXT文件就不会看到乱码了。  当然也可以用MultiByteToWideChar和WideCharToMultiByte转换成GBK编码再写

user: user-3
content: utf-8 非常不自然， wide char 自然但是和 ASCII不兼容，所以才有那么多麻烦

user: user-0
content: 我在发送http请求的时候，加入Accept-Charset: GB2312 http响应报文，仍然还是 Content-Type: text/xml;charset=utf-8 Error-Code: 0 Error-Text:   我能不能这种方式得到GB2312编码的汉字，发送GB2312 http请求

user: user-4
content: wide char就是个过渡的垃圾。  为什么不将源文件保存为utf-8，然后 gcc sourcefile.c -finput_charset = utf-8 fexec-charset = gbk

user: user-0
content: 顶层软件C++ java我不太懂啊，我用C语言和汇编。Linux GCC用的最多。

user: user-5
content: MultiByteToWideChar The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.   int MultiByteToWideChar(   UINT CodePage,         // code page   DWORD dwFlags,         // character-type options   LPCSTR lpMultiByteStr, // address of string to map   int cchMultiByte,      // number of bytes in string   LPWSTR lpWideCharStr,  // address of wide-character buffer   int cchWideChar        // size of buffer );   Parameters CodePage  Specifies the code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system. You can also specify one of the following values: Value Meaning  CP_ACP ANSI code page  CP_MACCP Macintosh code page  CP_OEMCP OEM code page  CP_SYMBOL Symbol code page (42)  CP_THREAD_ACP The current thread's ANSI code page  CP_UTF7 Translate using UTF-7  CP_UTF8 Translate using UTF-8    dwFlags  A set of bit flags that indicate whether to translate to precomposed or composite wide characters (if a composite form exists), whether to use glyph characters in place of control characters, and how to deal with invalid characters. You can specify a combination of the following flag constants: Value Meaning  MB_PRECOMPOSED Always use precomposed characters — that is, characters in which a base character and a nonspacing character have a single character value. This is the default translation option. Cannot be used with MB_COMPOSITE.  MB_COMPOSITE Always use composite characters — that is, characters in which a base character and a nonspacing character have different character values. Cannot be used with MB_PRECOMPOSED.  MB_ERR_INVALID_CHARS If the function encounters an invalid input character, it fails and GetLastError returns ERROR_NO_UNICODE_TRANSLATION.   MB_USEGLYPHCHARS Use glyph characters instead of control characters.    A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/non-spacing character combination. In the character è, the e is the base character and the accent grave mark is the nonspacing character.   The function's default behavior is to translate to the precomposed form. If a precomposed form does not exist, the function attempts to translate to a composite form.   The flags MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. The MB_USEGLYPHCHARS flag and the MB_ERR_INVALID_CHARS can be set regardless of the state of the other flags.   lpMultiByteStr  Points to the character string to be converted.  cchMultiByte  Specifies the size in bytes of the string pointed to by the lpMultiByteStr parameter. If this value is –1, the string is assumed to be null terminated and the length is calculated automatically.  lpWideCharStr  Points to a buffer that receives the translated string.  cchWideChar  Specifies the size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter. If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer.  Return Values If the function succeeds, and cchWideChar is nonzero, the return value is the number of wide characters written to the buffer pointed to by lpWideCharStr.   If the function succeeds, and cchWideChar is zero, the return value is the required size, in wide characters, for a buffer that can receive the translated string.   If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError may return one of the following error codes:  ERROR_INSUFFICIENT_BUFFER ERROR_INVALID_FLAGS ERROR_INVALID_PARAMETER ERROR_NO_UNICODE_TRANSLATION  Remarks The lpMultiByteStr and lpWideCharStr pointers must not be the same. If they are the same, the function fails, and GetLastError returns the value ERROR_INVALID_PARAMETER.   The function fails if MB_ERR_INVALID_CHARS is set and it encounters an invalid character in the source string. An invalid character is one that would translate to the default character if MB_ERR_INVALID_CHARS was not set, but is not the default character in the source string, or when a lead byte is found in a string and there is no valid trail byte for DBCS strings. When an invalid character is found, and MB_ERR_INVALID_CHARS is set, the function returns 0 and sets GetLastError with the error ERROR_NO_UNICODE_TRANSLATION.  Windows CE: Windows CE does not support the CP_UTF7 and CP_UTF8 values for the CodePage parameter.  QuickInfo   Windows NT: Requires version 3.1 or later.   Windows: Requires Windows 95 or later.   Windows CE: Requires version 1.0 or later.   Header: Declared in winnls.h.   Import Library: Use kernel32.lib.  See Also Unicode and Character Sets Overview, Unicode and Character Set Functions, WideCharToMultiByte