user-0 提问: 一串文字里面包含单字节和双字节，请转换为双字节输出

回复:
user: user-1
content: 支持zhao4zhong1和sholber（排名不分先后）

user: user-2
content: 这，编码之间的转换...

user: user-3
content: String Manipulation These routines operate on null-terminated single-byte character, wide-character, and multibyte-character strings. Use the buffer-manipulation routines, described in Buffer Manipulation, to work with character arrays that do not end with a null character.  String-Manipulation Routines  Routine Use  _mbscoll, _mbsicoll, _mbsncoll, _mbsnicoll Compare two multibyte-character strings using multibyte code page information (_mbsicoll and _mbsnicoll are case-insensitive)  _mbsdec, _strdec, _wcsdec Move string pointer back one character  _mbsinc, _strinc, _wcsinc Advance string pointer by one character  _mbslen Get number of multibyte characters in multibyte-character string; dependent upon OEM code page  _mbsnbcat Append, at most, first n bytes of one multibyte-character string to another  _mbsnbcmp Compare first n bytes of two multibyte-character strings  _mbsnbcnt Return number of multibyte-character bytes within supplied character count  _mbsnbcpy Copy n bytes of string  _mbsnbicmp Compare n bytes of two multibyte-character strings, ignoring case  _mbsnbset Set first n bytes of multibyte-character string to specified character  _mbsnccnt Return number of multibyte characters within supplied byte count  _mbsnextc, _strnextc, _wcsnextc Find next character in string  _mbsninc. _strninc, _wcsninc  Advance string pointer by n characters  _mbsspnp, _strspnp, _wcsspnp Return pointer to first character in given string that is not in another given string  _mbstrlen Get number of multibyte characters in multibyte-character string; locale-dependent  sprintf, _stprintf Write formatted data to a string  strcat, wcscat, _mbscat Append one string to another  strchr, wcschr, _mbschr Find first occurrence of specified character in string  strcmp, wcscmp, _mbscmp Compare two strings  strcoll, wcscoll, _stricoll, _wcsicoll, _strncoll, _wcsncoll, _strnicoll, _wcsnicoll Compare two strings using current locale code page information (_stricoll, _wcsicoll, _strnicoll, and _wcsnicoll are case-insensitive)  strcpy, wcscpy, _mbscpy Copy one string to another  strcspn, wcscspn, _mbscspn,  Find first occurrence of character from specified character set in string  _strdup, _wcsdup, _mbsdup Duplicate string  strerror Map error number to message string  _strerror Map user-defined error message to string  strftime, wcsftime Format date-and-time string  _stricmp, _wcsicmp, _mbsicmp Compare two strings without regard to case  strlen, wcslen, _mbslen, _mbstrlen Find length of string  _strlwr, _wcslwr, _mbslwr Convert string to lowercase  strncat, wcsncat, _mbsncat Append characters of string  strncmp, wcsncmp, _mbsncmp Compare characters of two strings  strncpy, wcsncpy, _mbsncpy Copy characters of one string to another  _strnicmp, _wcsnicmp, _mbsnicmp Compare characters of two strings without regard to case  _strnset, _wcsnset, _mbsnset Set first n characters of string to specified character  strpbrk, wcspbrk, _mbspbrk Find first occurrence of character from one string in another string  strrchr, wcsrchr,_mbsrchr Find last occurrence of given character in string  _strrev, _wcsrev,_mbsrev Reverse string  _strset, _wcsset, _mbsset Set all characters of string to specified character  strspn, wcsspn, _mbsspn Find first substring from one string in another string  strstr, wcsstr, _mbsstr Find first occurrence of specified string in another string  strtok, wcstok, _mbstok Find next token in string  _strupr, _wcsupr, _mbsupr Convert string to uppercase  strxfrm, wcsxfrm Transform string into collated form based on locale-specific information  vsprintf, _vstprint Write formatted output using a pointer to a list of arguments

user: user-4
content: char buf[] = "小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。"   //多字节   即有单字节, 也有双字节  wchar_t  wbuf[0x100] = {0};  //双字节  然后使用这个函数, 就可以把  buf转换到wbuf中. MultiByteToWideChar

user: user-5
content: LZ的意思应该类似全角，半角

user: user-3
content: 这个勉强算“双性恋”吧： #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32     #include <windows.h>     #include <io.h> #else     #include <unistd.h>     #include <sys/time.h>     #include <pthread.h>     #define  CRITICAL_SECTION   pthread_mutex_t     #define  _vsnprintf         vsnprintf #endif //Log{ #define MAXLOGSIZE 20000000 #define MAXLINSIZE 16000 #include <time.h> #include <sys/timeb.h> #include <stdarg.h> char logfilename1[]="MyLog1.log"; char logfilename2[]="MyLog2.log"; static char logstr[MAXLINSIZE+1]; char datestr[16]; char timestr[16]; char mss[4]; CRITICAL_SECTION cs_log; FILE *flog; #ifdef WIN32 void Lock(CRITICAL_SECTION *l) {     EnterCriticalSection(l); } void Unlock(CRITICAL_SECTION *l) {     LeaveCriticalSection(l); } #else void Lock(CRITICAL_SECTION *l) {     pthread_mutex_lock(l); } void Unlock(CRITICAL_SECTION *l) {     pthread_mutex_unlock(l); } #endif void LogV(const char *pszFmt,va_list argp) {     struct tm *now;     struct timeb tb;      if (NULL==pszFmt||0==pszFmt[0]) return;     _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,logstr);     flog=fopen(logfilename1,"a");     if (NULL!=flog) {         fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);         if (ftell(flog)>MAXLOGSIZE) {             fclose(flog);             if (rename(logfilename1,logfilename2)) {                 remove(logfilename2);                 rename(logfilename1,logfilename2);             }         } else {             fclose(flog);         }     } } void Log(const char *pszFmt,...) {     va_list argp;      Lock(&cs_log);     va_start(argp,pszFmt);     LogV(pszFmt,argp);     va_end(argp);     Unlock(&cs_log); } //Log} int main(int argc,char * argv[]) {     int i; #ifdef WIN32     InitializeCriticalSection(&cs_log); #else     pthread_mutex_init(&cs_log,NULL); #endif     for (i=0;i<10000;i++) {         Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);     } #ifdef WIN32     DeleteCriticalSection(&cs_log); #else     pthread_mutex_destroy(&cs_log); #endif     return 0; } //1-78行添加到你带main的.c或.cpp的那个文件的最前面 //81-85行添加到你的main函数开头 //89-93行添加到你的main函数结束前 //在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中

user: user-3
content: 引用 14 楼 sholber 的回复:[quote=引用 12 楼 zhao4zhong1 的回复:] 一件编码问题引发的血案！  给俺一个案例，证明赵4老师离开Windows也能活得很好。 嘎嘎~~[/quote] 说“跨平台一定比不跨平台好”就和说“双性恋一定比异性恋好”一样！

user: user-6
content: 引用 12 楼 zhao4zhong1 的回复:一件编码问题引发的血案！  给俺一个案例，证明赵4老师离开Windows也能活得很好。 嘎嘎~~

user: user-6
content: 引用 12 楼 zhao4zhong1 的回复:一件编码问题引发的血案！ # include <stdio.h>  int main (void) {     printf (u8"赵4老湿好诗！\n"); }

user: user-3
content: 一件编码问题引发的血案！

user: user-3
content: 再捅9楼一刀  在被虐中成长的sholber！ #pragma comment(lib,"user32") #pragma comment(lib,"gdi32") #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> extern "C" HWND WINAPI GetConsoleWindow(); void HideTheCursor() {     CONSOLE_CURSOR_INFO cciCursor;     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);      if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {         cciCursor.bVisible = FALSE;         SetConsoleCursorInfo(hStdOut, &cciCursor);     } } void ShowTheCursor() {     CONSOLE_CURSOR_INFO cciCursor;     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);      if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {         cciCursor.bVisible = TRUE;         SetConsoleCursorInfo(hStdOut, &cciCursor);     } } int main() {     HWND  hwnd;     HDC   hdc;     HFONT hfont;     wchar_t wc[2];      system("color F0");     system("cls");     HideTheCursor();     hwnd  = GetConsoleWindow();     hdc   = GetDC(hwnd);     hfont = CreateFont(48,0,0,0,0,0,0,0,GB2312_CHARSET ,0,0,0,0,"宋体-方正超大字符集");     SelectObject(hdc,hfont);     wc[0]=0xD854u;     wc[1]=0xDC00u;     TextOutW(hdc,10,10,wc,2);     DeleteObject(hfont);     ReleaseDC(hwnd,hdc);     getch();     system("color 07");     system("cls");     ShowTheCursor();     return 0; } #if 0 代理项或代理项对是一对共同表示单个字符的 16 位 Unicode 编码值。需要记住的关键一点是： 代理项对实际上是 32 位单个字符，不能再假定一个 16 位 Unicode 编码值正好映射到一个字符。  使用代理项对 代理项对的第一个值是高代理项，包含介于 U+D800 到 U+DBFF 范围内的 16 位代码值。 该对的第二个值是低代理项，包含介于 U+DC00 到 U+DFFF 范围内的值。通过使用代理项对， 16 位 Unicode 编码系统可以对已由 Unicode 标准定义的一百多万个其他字符 (220) 进行寻址。  在传递给 XmlTextWriter 方法的任何字符串中都可以使用代理项字符。不过，代理项字符在编写的 XML 中应该有效。例如，万维网联合会 (W3C) 建议不允许在元素或属性的名称中使用代理项字符。 如果字符串包含无效的代理项对，则引发异常。  另外，可以使用 WriteSurrogateCharEntity 写出与代理项对相对应的字符实体。字符实体以十六 进制格式写出，并用以下公式生成：  (highChar -0xD800) * 0x400 + (lowChar -0xDC00) + 0x10000  如果字符串包含无效的代理项对，则引发异常。下面的示例显示将代理项对作为输入的 WriteSurrogateCharEntity 方法。  C#复制  // The following line writes 𐀀. WriteSurrogateCharEntity ('\uDC00', '\uD800'); 下面的示例生成一个代理项对文件，将其加载到 XmlReader 中，并用新的文件名保存文件。 然后，原始文件和新文件被加载回应用程序的 XML 文档对象模型 (DOM) 结构中以进行比较。  C#复制  char lowChar, highChar; char [] charArray = new char[10]; FileStream targetFile = new FileStream("SurrogatePair.xml",       FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);  lowChar = Convert.ToChar(0xDC00); highChar = Convert.ToChar(0xD800); XmlTextWriter tw = new XmlTextWriter(targetFile, null); tw.Formatting = Formatting.Indented; tw.WriteStartElement("root"); tw.WriteStartAttribute("test", null); tw.WriteSurrogateCharEntity(lowChar, highChar); lowChar = Convert.ToChar(0xDC01); highChar = Convert.ToChar(0xD801); tw.WriteSurrogateCharEntity(lowChar, highChar); lowChar = Convert.ToChar(0xDFFF); highChar = Convert.ToChar(0xDBFF); tw.WriteSurrogateCharEntity(lowChar, highChar);  // Add 10 random surrogate pairs. // As Unicode, the high bytes are in lower // memory; for example, word 6A21 as 21 6A. // The high or low is in the logical sense. Random random = new Random(); for (int i = 0; i < 10; ++i) {       lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));       highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));       charArray[i] = highChar;       charArray[++i] = lowChar; } tw.WriteChars(charArray, 0, charArray.Length);  for (int i = 0; i < 10; ++i) {       lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));       highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));       tw.WriteSurrogateCharEntity(lowChar, highChar); }  tw.WriteEndAttribute(); tw.WriteEndElement(); tw.Flush(); tw.Close();  XmlTextReader r = new XmlTextReader("SurrogatePair.xml");  r.Read(); r.MoveToFirstAttribute(); targetFile = new FileStream("SurrogatePairFromReader.xml",        FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);  tw = new XmlTextWriter(targetFile, null); tw.Formatting = Formatting.Indented; tw.WriteStartElement("root"); tw.WriteStartAttribute("test", null); tw.WriteString(r.Value); tw.WriteEndAttribute(); tw.WriteEndElement(); tw.Flush(); tw.Close();  // Load both result files into the DOM and compare. XmlDocument doc1 = new XmlDocument(); XmlDocument doc2 = new XmlDocument(); doc1.Load("SurrogatePair.xml"); doc2.Load("SurrogatePairFromReader.xml"); if (doc1.InnerXml != doc2.InnerXml) {       Console.WriteLine("Surrogate Pair test case failed"); } 在使用 WriteChars 方法（一次写出一个缓冲区的数据）写出时，输入中的代理项对可能 会在一个缓冲区内被意外拆分。由于代理项值是定义完善的，如果 WriteChars 遇到来自 较低范围或者较高范围的 Unicode 值，它将该值标识为代理项对的一半。当遇到  WriteChars 将导致从拆分代理项对的缓冲区写入的情况时，将引发异常。使用   IsHighSurrogate 方法检查缓冲区是否以高代理项字符结束。如果缓冲区中的最后一个   字符不是高代理项，可以将该缓冲区传递给 WriteChars 方法。  请参见 概念 使用 XmlTextWriter 创建格式正确的 XML XmlTextWriter 的 XML 输出格式设置 XmlTextWriter 的命名空间功能  #endif

user: user-3
content: 9楼在我面前谈编码，好似关公面前耍大刀： 搜“ConvertZ”  先http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’，再点立即安装  再参考： C:\>dir /w "C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\cvt"  驱动器 C 中的卷是 C_HD5_1  卷的序列号是 1817-D526   C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\cvt 的目录  [..]         [.]          8859_1       8859_10      8859_13      8859_14      8859_15      8859_16      8859_2 8859_3       8859_4       8859_5       8859_6       8859_7       8859_8       8859_9       baltic       big5 cp037        cp1006       cp1026       cp1250       cp1251       cp1252       cp1253       cp1254       cp1255 cp1256       cp1257       cp1258       cp424        cp437        cp500        cp737        cp775        cp850 cp852        cp855        cp856        cp857        cp860        cp861        cp862        cp863        cp864 cp865        cp866        cp869        cp874        cp875        cp932        cp936        cp949        cp950 cyrillic     ebcdic       euc          euc_0208     gb12345      gb2312       greek        iceland      jis jis0201      jis_0208     ksc5601      latin2       one_one      roman        sjis         sjis_0208    turkish utf16        utf8         utf8_utf16   wbuffer      wstring      xjis         xone_byte    xtest        xtwo_byte               79 个文件      4,638,592 字节                2 个目录 12,588,097,536 可用字节

user: user-6
content: 引用 8 楼 zhao4zhong1 的回复:#include <stdio.h> #include <locale.h> #include <windows.h> char s[]="小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。"; wchar_t *ws; void HexDump(char *buf,int len,int addr) {     int i,j,k;     char binstr[80];      for (i=0;i<len;i++) {         if (0==(i%16)) {             sprintf(binstr,"%08x -",i+addr);             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         } else if (15==(i%16)) {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);             sprintf(binstr,"%s  ",binstr);             for (j=i-15;j<=i;j++) {                 sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');             }             printf("%s\n",binstr);         } else {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         }     }     if (0!=(i%16)) {         k=16-(i%16);         for (j=0;j<k;j++) {             sprintf(binstr,"%s   ",binstr);         }         sprintf(binstr,"%s  ",binstr);         k=16-k;         for (j=i-k;j<i;j++) {             sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');         }         printf("%s\n",binstr);     } } int main() {     setlocale(LC_ALL,"chs");     printf("%s\n",s);     HexDump(s,strlen(s)+1,(int)s);     int nLen = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, NULL);     ws = new wchar_t[nLen];     MultiByteToWideChar(CP_ACP, 0, s, -1, ws, nLen);     wprintf(L"%s\n",ws);     HexDump((char *)ws,wcslen(ws)*2+2,(int)ws);     delete[] ws;     return 0; } //小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。 //00414410 - d0 a1 cd ac d6 be c4 e3 ba c3 a3 a1 c7 eb ce ca  ................ //00414420 - d2 bb cf c2 2c c4 e3 d6 aa b5 c0 c8 cb c3 f1 b1  ....,........... //00414430 - b1 c2 b7 32 39 ba c5 d4 f5 c3 b4 d7 df d1 bd 3f  ...29..........? //00414440 - 2e 2e 2e 2e 2e 2e 2e 2e 2e 2e a1 a3 00           ............. //小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。 //00422420 - 0f 5c 0c 54 d7 5f 60 4f 7d 59 01 ff f7 8b ee 95  .\.T._`O}Y...... //00422430 - 00 4e 0b 4e 2c 00 60 4f e5 77 53 90 ba 4e 11 6c  .N.N,.`O.wS..N.l //00422440 - 17 53 ef 8d 32 00 39 00 f7 53 0e 60 48 4e 70 8d  .S..2.9..S.`HNp. //00422450 - 40 54 3f 00 2e 00 2e 00 2e 00 2e 00 2e 00 2e 00  @T?............. //00422460 - 2e 00 2e 00 2e 00 2e 00 02 30 00 00              .........0.. //   事情没这么简单的。 在符合楼主要求的前提下，考虑一下，从GB2312转换到UTF-16。 16位编码方案很多。 现在的C不仅仅支持char和wide char。 你积攒的东西需要更新了。

user: user-3
content: #include <stdio.h> #include <locale.h> #include <windows.h> char s[]="小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。"; wchar_t *ws; void HexDump(char *buf,int len,int addr) {     int i,j,k;     char binstr[80];      for (i=0;i<len;i++) {         if (0==(i%16)) {             sprintf(binstr,"%08x -",i+addr);             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         } else if (15==(i%16)) {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);             sprintf(binstr,"%s  ",binstr);             for (j=i-15;j<=i;j++) {                 sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');             }             printf("%s\n",binstr);         } else {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         }     }     if (0!=(i%16)) {         k=16-(i%16);         for (j=0;j<k;j++) {             sprintf(binstr,"%s   ",binstr);         }         sprintf(binstr,"%s  ",binstr);         k=16-k;         for (j=i-k;j<i;j++) {             sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');         }         printf("%s\n",binstr);     } } int main() {     setlocale(LC_ALL,"chs");     printf("%s\n",s);     HexDump(s,strlen(s)+1,(int)s);     int nLen = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, NULL);     ws = new wchar_t[nLen];     MultiByteToWideChar(CP_ACP, 0, s, -1, ws, nLen);     wprintf(L"%s\n",ws);     HexDump((char *)ws,wcslen(ws)*2+2,(int)ws);     delete[] ws;     return 0; } //小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。 //00414410 - d0 a1 cd ac d6 be c4 e3 ba c3 a3 a1 c7 eb ce ca  ................ //00414420 - d2 bb cf c2 2c c4 e3 d6 aa b5 c0 c8 cb c3 f1 b1  ....,........... //00414430 - b1 c2 b7 32 39 ba c5 d4 f5 c3 b4 d7 df d1 bd 3f  ...29..........? //00414440 - 2e 2e 2e 2e 2e 2e 2e 2e 2e 2e a1 a3 00           ............. //小同志你好！请问一下,你知道人民北路29号怎么走呀?..........。 //00422420 - 0f 5c 0c 54 d7 5f 60 4f 7d 59 01 ff f7 8b ee 95  .\.T._`O}Y...... //00422430 - 00 4e 0b 4e 2c 00 60 4f e5 77 53 90 ba 4e 11 6c  .N.N,.`O.wS..N.l //00422440 - 17 53 ef 8d 32 00 39 00 f7 53 0e 60 48 4e 70 8d  .S..2.9..S.`HNp. //00422450 - 40 54 3f 00 2e 00 2e 00 2e 00 2e 00 2e 00 2e 00  @T?............. //00422460 - 2e 00 2e 00 2e 00 2e 00 02 30 00 00              .........0.. //

user: user-7
content: 汉字是双 数字是单 吧

user: user-8
content: 整串文字整体来的，不会是一部分多字节，一部分宽字节的，看你怎么存了，如果是char数组，那么汉字就是占2个字节存，数字占1个，如果是wchar_t数组，那么汉字和数字都是2个字节存的

user: user-9
content: 哪个是单字节？哪个又是双字节？鄙人浅陋