指定存储文件的编码格式(上)

 

终于搞完了,内容稍微有点多分为两篇把。

《指定存储文件的编码格式(上)》

《指定存储文件的编码格式(下)》

 

本篇为上篇。

 

主流的文件编码包括:UTF8\UTF8-WITHOUT-BOM\UTF16LE\UTF16BE\ANSI等。

中文的windows操作系统默认使用就是ANSI编码。

各种编码的主要规则大家可以去wiki网站、unicode.org网站等查看。

本文的上篇和下篇都采用windows函数WideCharToMultiByte和MultiByteToWideChar为基础进行编写的。

本文的上篇和下篇主要完成从指定文件A中读取数据,输出到指定编码的文件B中。

之所以分成上下两篇主要是下篇是对上篇的改进和优化。

本篇源码主要片段:

   1 // test__OutputUtf8File.cpp : 定义控制台应用程序的入口点。

   2 //

   3 

   4 #include "stdafx.h"

   5 #include <windows.h>

   6 #include <string>

   7 #include <iostream>

   8 

   9 #define UTF8_SIGN 3

  10 #define UTF16_SIGN 2

  11 

  12 

  13 

  14 //************************************

  15 // Method:    Utf16leFileToUtf8File

  16 // FullName:  Utf16leFileToUtf8File

  17 // Access:    public 

  18 // Returns:   BOOL

  19 // Qualifier:将lpUtf16leFile文件内容写入到lpUtf8File文件中

  20 // Parameter: CONST LPTSTR lpUtf16leFile:输入文件utf16le编码

  21 // Parameter: CONST LPTSTR lpUtf8File:输出文件为utf8-with-BOM编码

  22 // *注:lpUtf16leFile文件只读;lpUtf8File文件总是创建或覆盖

  23 //************************************

  24 BOOL Utf16leFileToUtf8File(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf8File)

  25 {

  26     HANDLE hUtf16leFile = NULL;

  27     HANDLE hUtf8File = NULL;

  28 

  29 

  30     //create file

  31     hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

  32     if (INVALID_HANDLE_VALUE == hUtf16leFile)

  33     {

  34         int errLogNumber = GetLastError();

  35         printf_s("error number:%d\n", errLogNumber);

  36         return FALSE;

  37     }

  38 

  39 

  40     //read UTF16LE encode file content

  41     LPWSTR lpReadContentByUTF16 = NULL;

  42     DWORD cbReadContentByUTF16 = 0;

  43     DWORD cbPreReadContentByUTF16 = 0;

  44     DWORD cchReadContentByUTF16 = 0;

  45 

  46     cbReadContentByUTF16 = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END);

  47     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16)

  48     {

  49         int errLogNumber = GetLastError();

  50         printf_s("error number:%d\n", errLogNumber);

  51         ::CloseHandle(hUtf16leFile);

  52         return FALSE;

  53     }

  54     lpReadContentByUTF16 = (WCHAR *)malloc(cbReadContentByUTF16);

  55     if (NULL == lpReadContentByUTF16)

  56     {

  57         printf_s("malloc error\n");

  58         ::CloseHandle(hUtf16leFile);

  59         return FALSE;

  60     }

  61     ZeroMemory(lpReadContentByUTF16, cbReadContentByUTF16);

  62     SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN);

  63     if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16, cbReadContentByUTF16, &cbPreReadContentByUTF16, NULL))

  64     {

  65         int errLogNumber = GetLastError();

  66         printf_s("error number:%d\n", errLogNumber);

  67         free(lpReadContentByUTF16);

  68         ::CloseHandle(hUtf16leFile);

  69         return FALSE;

  70     }

  71     cchReadContentByUTF16 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 / sizeof(WCHAR) + 1) : (cbReadContentByUTF16 / sizeof(WCHAR)));

  72 

  73 

  74     //transform encode

  75     LPSTR lpWriteContentByUTF8 = NULL;

  76     DWORD cchWriteContentByUTF8 = 0;

  77     DWORD cbWriteContentByUTF8 = 0;

  78     DWORD cbPreWriteContentByUTF8 = 0;

  79 

  80     cbWriteContentByUTF8 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 + sizeof(WCHAR)) : (cbReadContentByUTF16));

  81     cchWriteContentByUTF8 = cchReadContentByUTF16;

  82     lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8);

  83     if (NULL == lpWriteContentByUTF8)

  84     {

  85         printf_s("malloc error\n");

  86         free(lpReadContentByUTF16);

  87         ::CloseHandle(hUtf16leFile);

  88         return FALSE;

  89     }

  90     ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8);

  91     if (0 == WideCharToMultiByte(CP_UTF8, 0, lpReadContentByUTF16, cchReadContentByUTF16, lpWriteContentByUTF8, cbWriteContentByUTF8, NULL, NULL))

  92     {

  93         printf_s("transform error\n");

  94         free(lpReadContentByUTF16);

  95         free(lpWriteContentByUTF8);

  96         ::CloseHandle(hUtf16leFile);

  97         return FALSE;

  98     }

  99 

 100 

 101     //write UTF8 encode file content

 102     hUtf8File = ::CreateFile(lpUtf8File, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 103     if (INVALID_HANDLE_VALUE == hUtf8File)

 104     {

 105         printf("Terminal failure: Unable to write to file.\n");

 106         free(lpReadContentByUTF16);

 107         free(lpWriteContentByUTF8);

 108         ::CloseHandle(hUtf16leFile);

 109         return FALSE;

 110     }

 111     for (int i = 0; i != cbWriteContentByUTF8; ++i)

 112     {

 113         if (TEXT('\0')==lpWriteContentByUTF8[i])

 114         {

 115             cchWriteContentByUTF8 = i;

 116             //lpWriteContentByUTF8[i] = 0;//设置结束符,但是utf8好像不用把

 117             break;

 118         }

 119     }

 120     SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN);

 121     if (FALSE == WriteFile(hUtf8File, lpWriteContentByUTF8, cchWriteContentByUTF8, &cbPreWriteContentByUTF8, NULL))

 122     {

 123         int errLogNumber = GetLastError();

 124         printf_s("error number:%d\n", errLogNumber);

 125         free(lpReadContentByUTF16);

 126         free(lpWriteContentByUTF8);

 127         ::CloseHandle(hUtf16leFile);

 128         return FALSE;

 129     }

 130 

 131 

 132     //release resource

 133     free(lpReadContentByUTF16);

 134     free(lpWriteContentByUTF8);

 135     ::CloseHandle(hUtf16leFile);

 136     ::CloseHandle(hUtf8File);

 137 

 138     return TRUE;

 139 

 140 }

 141 

 142 //************************************

 143 // Method:    Utf16leFileToUtf8NoBOMFile

 144 // FullName:  Utf16leFileToUtf8NoBOMFile

 145 // Access:    public 

 146 // Returns:   BOOL

 147 // Qualifier:将lpUtf16leFile文件内容写入到lpUtf8NoBOMFile文件中

 148 // Parameter: CONST LPTSTR lpUtf16leFile:输入文件utf16le编码

 149 // Parameter: CONST LPTSTR lpUtf8NoBOMFile:输出文件为utf8-without-BOM编码

 150 // *注:lpUtf16leFile文件只读;lpUtf8NoBOMFile文件总是创建或覆盖

 151 //************************************

 152 BOOL Utf16leFileToUtf8NoBOMFile(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf8NoBOMFile)

 153 {

 154     HANDLE hUtf16leFile = NULL;

 155     HANDLE hUtf8NoBOMFile = NULL;

 156 

 157 

 158     //create UTF16LE file

 159     hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 160     if (INVALID_HANDLE_VALUE == hUtf16leFile)

 161     {

 162         int errLogNumber = GetLastError();

 163         printf_s("error number:%d\n", errLogNumber);

 164         return FALSE;

 165     }

 166 

 167 

 168     //read UTF16LE encode file content

 169     LPWSTR lpReadContentByUTF16 = NULL;

 170     DWORD cbReadContentByUTF16 = 0;

 171     DWORD cbPreReadContentByUTF16 = 0;

 172     DWORD cchReadContentByUTF16 = 0;

 173 

 174     cbReadContentByUTF16 = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END);

 175     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16)

 176     {

 177         int errLogNumber = GetLastError();

 178         printf_s("error number:%d\n", errLogNumber);

 179         ::CloseHandle(hUtf16leFile);

 180         return FALSE;

 181     }

 182     lpReadContentByUTF16 = (WCHAR *)malloc(cbReadContentByUTF16);

 183     if (NULL == lpReadContentByUTF16)

 184     {

 185         printf_s("malloc error\n");

 186         ::CloseHandle(hUtf16leFile);

 187         return FALSE;

 188     }

 189     ZeroMemory(lpReadContentByUTF16, cbReadContentByUTF16);

 190     SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN);

 191     if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16, cbReadContentByUTF16, &cbPreReadContentByUTF16, NULL))

 192     {

 193         int errLogNumber = GetLastError();

 194         printf_s("error number:%d\n", errLogNumber);

 195         free(lpReadContentByUTF16);

 196         ::CloseHandle(hUtf16leFile);

 197         return FALSE;

 198     }

 199     cchReadContentByUTF16 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 / sizeof(WCHAR) + 1) : (cbReadContentByUTF16 / sizeof(WCHAR)));

 200 

 201 

 202     //transform encode

 203     LPSTR lpWriteContentByUTF8 = NULL;

 204     DWORD cchWriteContentByUTF8 = 0;

 205     DWORD cbWriteContentByUTF8 = 0;

 206     DWORD cbPreWriteContentByUTF8 = 0;

 207 

 208     cbWriteContentByUTF8 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 + sizeof(WCHAR)) : (cbReadContentByUTF16));

 209     cchWriteContentByUTF8 = cchReadContentByUTF16;

 210     lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8);

 211     if (NULL == lpWriteContentByUTF8)

 212     {

 213         printf_s("malloc error\n");

 214         free(lpReadContentByUTF16);

 215         ::CloseHandle(hUtf16leFile);

 216         return FALSE;

 217     }

 218     ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8);

 219     if (0 == WideCharToMultiByte(CP_UTF8, 0, lpReadContentByUTF16, cchReadContentByUTF16, lpWriteContentByUTF8, cbWriteContentByUTF8, NULL, NULL))

 220     {

 221         printf_s("transform error\n");

 222         free(lpReadContentByUTF16);

 223         free(lpWriteContentByUTF8);

 224         ::CloseHandle(hUtf16leFile);

 225         return FALSE;

 226     }

 227 

 228 

 229     //write UTF8NoBOM encode file content

 230     LPSTR lpWriteContentByUTF8NOBOM = NULL;

 231     DWORD cbWriteContentByUTF8NOBOM = 0;

 232 

 233     hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 234     if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile)

 235     {

 236         printf("Terminal failure: Unable to write to file.\n");

 237         free(lpReadContentByUTF16);

 238         free(lpWriteContentByUTF8);

 239         ::CloseHandle(hUtf16leFile);

 240         return FALSE;

 241     }

 242     for (int i = 0; i != cbWriteContentByUTF8; ++i)

 243     {

 244         if (TEXT('\0') == lpWriteContentByUTF8[i])

 245         {

 246             cbWriteContentByUTF8NOBOM = i - UTF8_SIGN;

 247             break;

 248         }

 249     }

 250     lpWriteContentByUTF8NOBOM = (CHAR *)malloc(cbWriteContentByUTF8NOBOM);

 251     if (NULL == lpWriteContentByUTF8NOBOM)

 252     {

 253         printf_s("malloc error\n");

 254         free(lpReadContentByUTF16);

 255         free(lpWriteContentByUTF8);

 256         ::CloseHandle(hUtf16leFile);

 257         return FALSE;

 258     }

 259     ZeroMemory(lpWriteContentByUTF8NOBOM, cbWriteContentByUTF8NOBOM);

 260     CopyMemory(lpWriteContentByUTF8NOBOM, lpWriteContentByUTF8 + UTF8_SIGN, cbWriteContentByUTF8NOBOM);

 261     SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN);

 262     if (FALSE == WriteFile(hUtf8NoBOMFile, lpWriteContentByUTF8NOBOM, cbWriteContentByUTF8NOBOM, &cbPreWriteContentByUTF8, NULL))

 263     {

 264         int errLogNumber = GetLastError();

 265         printf_s("error number:%d\n", errLogNumber);

 266         free(lpReadContentByUTF16);

 267         free(lpWriteContentByUTF8);

 268         free(lpWriteContentByUTF8NOBOM);

 269         ::CloseHandle(hUtf16leFile);

 270         return FALSE;

 271     }

 272 

 273 

 274     //release resource

 275     free(lpReadContentByUTF16);

 276     free(lpWriteContentByUTF8);

 277     free(lpWriteContentByUTF8NOBOM);

 278     ::CloseHandle(hUtf16leFile);

 279     ::CloseHandle(hUtf8NoBOMFile);

 280 

 281     return TRUE;

 282 

 283 }

 284 

 285 //************************************

 286 // Method:    Utf16leFileToUtf8NoBOMFile2

 287 // FullName:  Utf16leFileToUtf8NoBOMFile2

 288 // Access:    public 

 289 // Returns:   BOOL

 290 // Qualifier:

 291 // Parameter: CONST LPTSTR lpUtf16leFile:输入文件utf16le编码

 292 // Parameter: CONST LPTSTR lpUtf8NoBOMFile:输出文件为utf8-without-BOM编码

 293 // *注:lpUtf16leFile文件只读;lpUtf8NoBOMFile文件总是创建或覆盖

 294 //************************************

 295 BOOL Utf16leFileToUtf8NoBOMFile2(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf8NoBOMFile)

 296 {

 297     HANDLE hUtf16leFile = NULL;

 298     HANDLE hUtf8NoBOMFile = NULL;

 299 

 300 

 301     //create UTF16LE file

 302     hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 303     if (INVALID_HANDLE_VALUE == hUtf16leFile)

 304     {

 305         int errLogNumber = GetLastError();

 306         printf_s("error number:%d\n", errLogNumber);

 307         return FALSE;

 308     }

 309 

 310 

 311     //read UTF16LE encode file content

 312     LPWSTR lpReadContentByUTF16 = NULL;

 313     DWORD cbReadContentByUTF16 = 0;

 314     DWORD cbPreReadContentByUTF16 = 0;

 315     DWORD cchReadContentByUTF16 = 0;

 316 

 317     cbReadContentByUTF16 = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END);

 318     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16)

 319     {

 320         int errLogNumber = GetLastError();

 321         printf_s("error number:%d\n", errLogNumber);

 322         ::CloseHandle(hUtf16leFile);

 323         return FALSE;

 324     }

 325     lpReadContentByUTF16 = (WCHAR *)malloc(cbReadContentByUTF16);

 326     if (NULL == lpReadContentByUTF16)

 327     {

 328         printf_s("malloc error\n");

 329         ::CloseHandle(hUtf16leFile);

 330         return FALSE;

 331     }

 332     ZeroMemory(lpReadContentByUTF16, cbReadContentByUTF16);

 333     SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN);

 334     if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16, cbReadContentByUTF16, &cbPreReadContentByUTF16, NULL))

 335     {

 336         int errLogNumber = GetLastError();

 337         printf_s("error number:%d\n", errLogNumber);

 338         free(lpReadContentByUTF16);

 339         ::CloseHandle(hUtf16leFile);

 340         return FALSE;

 341     }

 342     cchReadContentByUTF16 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 / sizeof(WCHAR) + 1) : (cbReadContentByUTF16 / sizeof(WCHAR)));

 343 

 344 

 345     //transform encode

 346     LPSTR lpWriteContentByUTF8 = NULL;

 347     DWORD cchWriteContentByUTF8 = 0;

 348     DWORD cbWriteContentByUTF8 = 0;

 349     DWORD cbPreWriteContentByUTF8 = 0;

 350 

 351     cbWriteContentByUTF8 = ((cbReadContentByUTF16 % sizeof(WCHAR)) != 0 ? (cbReadContentByUTF16 + sizeof(WCHAR)) : (cbReadContentByUTF16));

 352     cchWriteContentByUTF8 = cchReadContentByUTF16;

 353     lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8);

 354     if (NULL == lpWriteContentByUTF8)

 355     {

 356         printf_s("malloc error\n");

 357         free(lpReadContentByUTF16);

 358         ::CloseHandle(hUtf16leFile);

 359         return FALSE;

 360     }

 361     ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8);

 362     if (0 == WideCharToMultiByte(CP_UTF8, 0, lpReadContentByUTF16, cchReadContentByUTF16, lpWriteContentByUTF8, cbWriteContentByUTF8, NULL, NULL))

 363     {

 364         printf_s("transform error\n");

 365         free(lpReadContentByUTF16);

 366         free(lpWriteContentByUTF8);

 367         ::CloseHandle(hUtf16leFile);

 368         return FALSE;

 369     }

 370 

 371 

 372     //write UTF8NOBOM file content

 373     hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 374     if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile)

 375     {

 376         printf("Terminal failure: Unable to write to file.\n");

 377         free(lpReadContentByUTF16);

 378         free(lpWriteContentByUTF8);

 379         ::CloseHandle(hUtf16leFile);

 380         return FALSE;

 381     }

 382     for (int i = 0; i != cbWriteContentByUTF8; ++i)

 383     {

 384         if (TEXT('\0') == lpWriteContentByUTF8[i])

 385         {

 386             cchWriteContentByUTF8 = i;

 387             break;

 388         }

 389     }

 390     SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN);

 391     if (FALSE == WriteFile(hUtf8NoBOMFile, lpWriteContentByUTF8 + UTF8_SIGN, cchWriteContentByUTF8 - UTF8_SIGN, &cbPreWriteContentByUTF8, NULL))

 392     {

 393         int errLogNumber = GetLastError();

 394         printf_s("error number:%d\n", errLogNumber);

 395         free(lpReadContentByUTF16);

 396         free(lpWriteContentByUTF8);

 397         ::CloseHandle(hUtf16leFile);

 398         ::CloseHandle(hUtf8NoBOMFile);

 399         return FALSE;

 400     }

 401 

 402 

 403     //release resource

 404     free(lpReadContentByUTF16);

 405     free(lpWriteContentByUTF8);

 406     ::CloseHandle(hUtf16leFile);

 407     ::CloseHandle(hUtf8NoBOMFile);

 408 

 409     return TRUE;

 410 }

 411 

 412 //************************************

 413 // Method:    Utf8FileToUtf16leFile

 414 // FullName:  Utf8FileToUtf16leFile

 415 // Access:    public 

 416 // Returns:   BOOL

 417 // Qualifier:将utf8编码格式文件转换为utf16le编码格式文件

 418 // Parameter: CONST LPTSTR lpUtf8File:utf8编码格式文件

 419 // Parameter: CONST LPTSTR lpUtf16leFile:utf16le编码格式文件

 420 //************************************

 421 BOOL Utf8FileToUtf16leFile(CONST LPTSTR lpUtf8File, CONST LPTSTR lpUtf16leFile)

 422 {

 423     HANDLE hUtf16leFile = NULL;

 424     HANDLE hUtf8File = NULL;

 425 

 426 

 427     //create UTF8 file

 428     hUtf8File = ::CreateFile(lpUtf8File, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 429     if (INVALID_HANDLE_VALUE == hUtf8File)

 430     {

 431         int errLogNumber = GetLastError();

 432         printf_s("error number:%d\n", errLogNumber);

 433         return FALSE;

 434     }

 435 

 436 

 437     //read UTF8 encode file content

 438     LPSTR lpReadContentByUTF8 = NULL;

 439     DWORD cbReadContentByUTF8 = 0;

 440     DWORD cbPreReadContentByUTF8 = 0;

 441     DWORD cchReadContentByUTF8 = 0;

 442 

 443     cbReadContentByUTF8 = SetFilePointer(hUtf8File, 0, NULL, FILE_END);

 444     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8)

 445     {

 446         int errLogNumber = GetLastError();

 447         printf_s("error number:%d\n", errLogNumber);

 448         ::CloseHandle(hUtf8File);

 449         return FALSE;

 450     }

 451     lpReadContentByUTF8 = (CHAR *)malloc(cbReadContentByUTF8);

 452     if (NULL == lpReadContentByUTF8)

 453     {

 454         printf_s("malloc error\n");

 455         ::CloseHandle(hUtf8File);

 456         return FALSE;

 457     }

 458     ZeroMemory(lpReadContentByUTF8, cbReadContentByUTF8);

 459     SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN);

 460     if (FALSE == ReadFile(hUtf8File, lpReadContentByUTF8, cbReadContentByUTF8, &cbPreReadContentByUTF8, NULL))

 461     {

 462         int errLogNumber = GetLastError();

 463         printf_s("error number:%d\n", errLogNumber);

 464         free(lpReadContentByUTF8);

 465         ::CloseHandle(hUtf8File);

 466         return FALSE;

 467     }

 468     if (sizeof(CHAR) != sizeof(BYTE))

 469     {

 470         free(lpReadContentByUTF8);

 471         ::CloseHandle(hUtf8File);

 472         return FALSE;

 473     }

 474     cchReadContentByUTF8 = cbReadContentByUTF8;

 475 

 476 

 477     //transform encode

 478     LPWSTR lpWriteContentByUTF16 = NULL;

 479     DWORD cchWriteContentByUTF16 = 0;

 480     DWORD cbWriteContentByUTF16 = 0;

 481     DWORD cbPreWriteContentByUTF16 = 0;

 482 

 483     cbWriteContentByUTF16 = cchReadContentByUTF8 * 2;

 484     cchWriteContentByUTF16 = cchReadContentByUTF8;

 485     lpWriteContentByUTF16 = (WCHAR *)malloc(cbWriteContentByUTF16);

 486     if (NULL == lpWriteContentByUTF16)

 487     {

 488         printf_s("malloc error\n");

 489         free(lpReadContentByUTF8);

 490         ::CloseHandle(hUtf8File);

 491         return FALSE;

 492     }

 493     ZeroMemory(lpWriteContentByUTF16, cbWriteContentByUTF16);

 494     if (0 == MultiByteToWideChar(CP_UTF8, 0, lpReadContentByUTF8, cbReadContentByUTF8, lpWriteContentByUTF16, cchWriteContentByUTF16))

 495     {

 496         printf_s("transform error\n");

 497         free(lpReadContentByUTF8);

 498         free(lpWriteContentByUTF16);

 499         ::CloseHandle(hUtf8File);

 500         return FALSE;

 501     }

 502 

 503 

 504     //write UTF16LE encode file content

 505     hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 506     if (INVALID_HANDLE_VALUE == hUtf16leFile)

 507     {

 508         printf("Terminal failure: Unable to write to file.\n");

 509         free(lpReadContentByUTF8);

 510         free(lpWriteContentByUTF16);

 511         ::CloseHandle(hUtf8File);

 512         return FALSE;

 513     }

 514     for (int i = 0; i != cbWriteContentByUTF16 -1; ++i)

 515     {

 516         if (0 == lpWriteContentByUTF16[i] && 0 == lpWriteContentByUTF16[i+1])

 517         {

 518             cbWriteContentByUTF16 = i;

 519             break;

 520         }

 521     }

 522     //////////////////////////////////////////////////////////////////////////

 523 

 524     //std::wstring wstrText = L"output to utf8 with BOM.\r\n输出到utf8带BOM的文件中。\r\nengli  sh";

 525     //int lgText = wstrText.length();

 526     //int lgText2 = wstrText.size();

 527     //std::cout << lgText << "" << lgText2 << std::endl;

 528 

 529     //////////////////////////////////////////////////////////////////////////

 530     SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN);

 531     if (FALSE == WriteFile(hUtf16leFile, lpWriteContentByUTF16, cbWriteContentByUTF16 * sizeof(WCHAR), &cbPreWriteContentByUTF16, NULL))

 532     {

 533         int errLogNumber = GetLastError();

 534         printf_s("error number:%d\n", errLogNumber);

 535         free(lpReadContentByUTF8);

 536         free(lpWriteContentByUTF16);

 537         ::CloseHandle(hUtf16leFile);

 538         ::CloseHandle(hUtf8File);

 539         return FALSE;

 540     }

 541 

 542 

 543     //release resource

 544     free(lpReadContentByUTF8);

 545     free(lpWriteContentByUTF16);

 546     ::CloseHandle(hUtf16leFile);

 547     ::CloseHandle(hUtf8File);

 548 

 549     return TRUE;

 550 

 551 }

 552 

 553 BOOL Utf8NoBOMFileToUtf16leFile(CONST LPTSTR lpUtfNoBOM8File, CONST LPTSTR lpUtf16leFile)

 554 {

 555     return Utf8FileToUtf16leFile(lpUtfNoBOM8File, lpUtf16leFile);

 556 

 557     //HANDLE hUtf16leFile = NULL;

 558     //HANDLE hUtf8File = NULL;

 559 

 560 

 561     ////create UTF8 file

 562     //hUtf8File = ::CreateFile(lpUtf8File, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 563     //if (INVALID_HANDLE_VALUE == hUtf8File)

 564     //{

 565     //    int errLogNumber = GetLastError();

 566     //    printf_s("error number:%d\n", errLogNumber);

 567     //    return FALSE;

 568     //}

 569 

 570 

 571     ////read UTF8 encode file content

 572     //LPSTR lpReadContentByUTF8 = NULL;

 573     //DWORD cbReadContentByUTF8 = 0;

 574     //DWORD cbPreReadContentByUTF8 = 0;

 575     //DWORD cchReadContentByUTF8 = 0;

 576 

 577     //cbReadContentByUTF8 = SetFilePointer(hUtf8File, 0, NULL, FILE_END);

 578     //if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8)

 579     //{

 580     //    int errLogNumber = GetLastError();

 581     //    printf_s("error number:%d\n", errLogNumber);

 582     //    ::CloseHandle(hUtf8File);

 583     //    return FALSE;

 584     //}

 585     //lpReadContentByUTF8 = (CHAR *)malloc(cbReadContentByUTF8);

 586     //if (NULL == lpReadContentByUTF8)

 587     //{

 588     //    printf_s("malloc error\n");

 589     //    ::CloseHandle(hUtf8File);

 590     //    return FALSE;

 591     //}

 592     //ZeroMemory(lpReadContentByUTF8, cbReadContentByUTF8);

 593     //SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN);

 594     //if (FALSE == ReadFile(hUtf8File, lpReadContentByUTF8, cbReadContentByUTF8, &cbPreReadContentByUTF8, NULL))

 595     //{

 596     //    int errLogNumber = GetLastError();

 597     //    printf_s("error number:%d\n", errLogNumber);

 598     //    free(lpReadContentByUTF8);

 599     //    ::CloseHandle(hUtf8File);

 600     //    return FALSE;

 601     //}

 602     //if (sizeof(CHAR) != sizeof(BYTE))

 603     //{

 604     //    free(lpReadContentByUTF8);

 605     //    ::CloseHandle(hUtf8File);

 606     //    return FALSE;

 607     //}

 608     //cchReadContentByUTF8 = cbReadContentByUTF8;

 609 

 610 

 611     ////transform encode

 612     //LPWSTR lpWriteContentByUTF16 = NULL;

 613     //DWORD cchWriteContentByUTF16 = 0;

 614     //DWORD cbWriteContentByUTF16 = 0;

 615     //DWORD cbPreWriteContentByUTF16 = 0;

 616 

 617     //cbWriteContentByUTF16 = cchReadContentByUTF8 * 2;

 618     //cchWriteContentByUTF16 = cchReadContentByUTF8;

 619     //lpWriteContentByUTF16 = (WCHAR *)malloc(cbWriteContentByUTF16);

 620     //if (NULL == lpWriteContentByUTF16)

 621     //{

 622     //    printf_s("malloc error\n");

 623     //    free(lpReadContentByUTF8);

 624     //    ::CloseHandle(hUtf8File);

 625     //    return FALSE;

 626     //}

 627     //ZeroMemory(lpWriteContentByUTF16, cbWriteContentByUTF16);

 628     //if (0 == MultiByteToWideChar(CP_UTF8, 0, lpReadContentByUTF8, cbReadContentByUTF8, lpWriteContentByUTF16, cchWriteContentByUTF16))

 629     //{

 630     //    printf_s("transform error\n");

 631     //    free(lpReadContentByUTF8);

 632     //    free(lpWriteContentByUTF16);

 633     //    ::CloseHandle(hUtf8File);

 634     //    return FALSE;

 635     //}

 636 

 637 

 638     ////write UTF16LE encode file content

 639     //hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 640     //if (INVALID_HANDLE_VALUE == hUtf16leFile)

 641     //{

 642     //    printf("Terminal failure: Unable to write to file.\n");

 643     //    free(lpReadContentByUTF8);

 644     //    free(lpWriteContentByUTF16);

 645     //    ::CloseHandle(hUtf8File);

 646     //    return FALSE;

 647     //}

 648     //for (int i = 0; i != cbWriteContentByUTF16 - 1; ++i)

 649     //{

 650     //    if (0 == lpWriteContentByUTF16[i] && 0 == lpWriteContentByUTF16[i + 1])

 651     //    {

 652     //        cbWriteContentByUTF16 = i;

 653     //        break;

 654     //    }

 655     //}

 656     ////////////////////////////////////////////////////////////////////////////

 657 

 658     ////std::wstring wstrText = L"output to utf8 with BOM.\r\n输出到utf8带BOM的文件中。\r\nengli  sh";

 659     ////int lgText = wstrText.length();

 660     ////int lgText2 = wstrText.size();

 661     ////std::cout << lgText << "" << lgText2 << std::endl;

 662 

 663     ////////////////////////////////////////////////////////////////////////////

 664     //SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN);

 665     //if (FALSE == WriteFile(hUtf16leFile, lpWriteContentByUTF16, cbWriteContentByUTF16 * sizeof(WCHAR), &cbPreWriteContentByUTF16, NULL))

 666     //{

 667     //    int errLogNumber = GetLastError();

 668     //    printf_s("error number:%d\n", errLogNumber);

 669     //    free(lpReadContentByUTF8);

 670     //    free(lpWriteContentByUTF16);

 671     //    ::CloseHandle(hUtf16leFile);

 672     //    ::CloseHandle(hUtf8File);

 673     //    return FALSE;

 674     //}

 675 

 676 

 677     ////release resource

 678     //free(lpReadContentByUTF8);

 679     //free(lpWriteContentByUTF16);

 680     //::CloseHandle(hUtf16leFile);

 681     //::CloseHandle(hUtf8File);

 682 

 683     //return TRUE;

 684 

 685 }

 686 

 687 BOOL Utf8FileToUtf8NoBOMFile(CONST LPTSTR lpUtf8File, CONST LPTSTR lpUtf8NoBOMFile)

 688 {

 689     HANDLE hUtf8NoBOMFile = NULL;

 690     HANDLE hUtf8File = NULL;

 691 

 692 

 693     //create file

 694     hUtf8File = ::CreateFile(lpUtf8File, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 695     if (INVALID_HANDLE_VALUE == hUtf8File)

 696     {

 697         int errLogNumber = GetLastError();

 698         printf_s("error number:%d\n", errLogNumber);

 699         return FALSE;

 700     }

 701 

 702 

 703     //read UTF8 encode file content

 704     LPSTR lpReadContentByUTF8 = NULL;

 705     DWORD cbReadContentByUTF8 = 0;

 706     DWORD cbPreReadContentByUTF8 = 0;

 707     DWORD cchReadContentByUTF8 = 0;

 708 

 709     cbReadContentByUTF8 = SetFilePointer(hUtf8File, 0, NULL, FILE_END);

 710     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8)

 711     {

 712         int errLogNumber = GetLastError();

 713         printf_s("error number:%d\n", errLogNumber);

 714         ::CloseHandle(hUtf8File);

 715         return FALSE;

 716     }

 717     lpReadContentByUTF8 = (CHAR *)malloc(cbReadContentByUTF8);

 718     if (NULL == lpReadContentByUTF8)

 719     {

 720         printf_s("malloc error\n");

 721         ::CloseHandle(hUtf8File);

 722         return FALSE;

 723     }

 724     ZeroMemory(lpReadContentByUTF8, cbReadContentByUTF8);

 725     SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN);

 726     if (FALSE == ReadFile(hUtf8File, lpReadContentByUTF8, cbReadContentByUTF8, &cbPreReadContentByUTF8, NULL))

 727     {

 728         int errLogNumber = GetLastError();

 729         printf_s("error number:%d\n", errLogNumber);

 730         free(lpReadContentByUTF8);

 731         ::CloseHandle(hUtf8File);

 732         return FALSE;

 733     }

 734     if (sizeof(CHAR) != sizeof(BYTE))

 735     {

 736         free(lpReadContentByUTF8);

 737         ::CloseHandle(hUtf8File);

 738         return FALSE;

 739     }

 740     cchReadContentByUTF8 = cbReadContentByUTF8;

 741     

 742 

 743     //write UTF8NoBOM encode file content

 744     LPSTR lpWriteContentByUTF8NoBOM = NULL;

 745     DWORD cbWriteContentByUTF8NoBOM = 0;

 746     DWORD cbPreWriteContentByUTF8NoBOM = 0;

 747     DWORD cchWriteContentByUTF8NoBOM = 0;

 748 

 749     cbWriteContentByUTF8NoBOM = cbReadContentByUTF8 - UTF8_SIGN;

 750     lpWriteContentByUTF8NoBOM = (CHAR *)malloc(cbWriteContentByUTF8NoBOM);

 751     if (NULL == lpWriteContentByUTF8NoBOM)

 752     {

 753         printf_s("malloc error\n");

 754         free(lpReadContentByUTF8);

 755         ::CloseHandle(hUtf8File);

 756         return FALSE;

 757     }

 758     ZeroMemory(lpWriteContentByUTF8NoBOM, cbWriteContentByUTF8NoBOM);

 759 

 760     CopyMemory(lpWriteContentByUTF8NoBOM, lpReadContentByUTF8 + UTF8_SIGN, cbWriteContentByUTF8NoBOM);

 761 

 762     hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 763     if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile)

 764     {

 765         printf("Terminal failure: Unable to write to file.\n");

 766         free(lpReadContentByUTF8);

 767         free(lpWriteContentByUTF8NoBOM);

 768         ::CloseHandle(hUtf8File);

 769         return FALSE;

 770     }

 771     SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN);

 772     if (FALSE == WriteFile(hUtf8NoBOMFile, lpWriteContentByUTF8NoBOM, cbWriteContentByUTF8NoBOM, &cbPreWriteContentByUTF8NoBOM, NULL))

 773     {

 774         int errLogNumber = GetLastError();

 775         printf_s("error number:%d\n", errLogNumber);

 776         free(lpReadContentByUTF8);

 777         free(lpWriteContentByUTF8NoBOM);

 778         ::CloseHandle(hUtf8NoBOMFile);

 779         ::CloseHandle(hUtf8File);

 780         return FALSE;

 781     }

 782 

 783 

 784     //release resource

 785     free(lpReadContentByUTF8);

 786     free(lpWriteContentByUTF8NoBOM);

 787     ::CloseHandle(hUtf8NoBOMFile);

 788     ::CloseHandle(hUtf8File);

 789 

 790     return TRUE;

 791 

 792 }

 793 

 794 BOOL Utf8NoBOMFileToUtf8File(CONST LPTSTR lpUtf8NoBOMFile, CONST LPTSTR lpUtf8File)

 795 {

 796     HANDLE hUtf8NoBOMFile = NULL;

 797     HANDLE hUtf8File = NULL;

 798 

 799 

 800     //create file

 801     hUtf8NoBOMFile = ::CreateFile(lpUtf8NoBOMFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 802     if (INVALID_HANDLE_VALUE == hUtf8NoBOMFile)

 803     {

 804         int errLogNumber = GetLastError();

 805         printf_s("error number:%d\n", errLogNumber);

 806         return FALSE;

 807     }

 808 

 809 

 810     //read UTF8 encode file content

 811     LPSTR lpReadContentByUTF8NoBOM = NULL;

 812     DWORD cbReadContentByUTF8NoBOM = 0;

 813     DWORD cbPreReadContentByUTF8NoBOM = 0;

 814     DWORD cchReadContentByUTF8NoBOM = 0;

 815 

 816     cbReadContentByUTF8NoBOM = SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_END);

 817     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF8NoBOM)

 818     {

 819         int errLogNumber = GetLastError();

 820         printf_s("error number:%d\n", errLogNumber);

 821         ::CloseHandle(hUtf8NoBOMFile);

 822         return FALSE;

 823     }

 824     lpReadContentByUTF8NoBOM = (CHAR *)malloc(cbReadContentByUTF8NoBOM);

 825     if (NULL == lpReadContentByUTF8NoBOM)

 826     {

 827         printf_s("malloc error\n");

 828         ::CloseHandle(hUtf8NoBOMFile);

 829         return FALSE;

 830     }

 831     ZeroMemory(lpReadContentByUTF8NoBOM, cbReadContentByUTF8NoBOM);

 832     SetFilePointer(hUtf8NoBOMFile, 0, NULL, FILE_BEGIN);

 833     if (FALSE == ReadFile(hUtf8NoBOMFile, lpReadContentByUTF8NoBOM, cbReadContentByUTF8NoBOM, &cbPreReadContentByUTF8NoBOM, NULL))

 834     {

 835         int errLogNumber = GetLastError();

 836         printf_s("error number:%d\n", errLogNumber);

 837         free(lpReadContentByUTF8NoBOM);

 838         ::CloseHandle(hUtf8NoBOMFile);

 839         return FALSE;

 840     }

 841     if (sizeof(CHAR) != sizeof(BYTE))

 842     {

 843         free(lpReadContentByUTF8NoBOM);

 844         ::CloseHandle(hUtf8NoBOMFile);

 845         return FALSE;

 846     }

 847     cchReadContentByUTF8NoBOM = cbReadContentByUTF8NoBOM;

 848 

 849 

 850     //write UTF8NoBOM encode file content

 851     LPSTR lpWriteContentByUTF8 = NULL;

 852     DWORD cbWriteContentByUTF8 = 0;

 853     DWORD cbPreWriteContentByUTF8 = 0;

 854     DWORD cchWriteContentByUTF8 = 0;

 855 

 856     cbWriteContentByUTF8 = cbReadContentByUTF8NoBOM + UTF8_SIGN;

 857     lpWriteContentByUTF8 = (CHAR *)malloc(cbWriteContentByUTF8);

 858     if (NULL == lpWriteContentByUTF8)

 859     {

 860         printf_s("malloc error\n");

 861         free(lpReadContentByUTF8NoBOM);

 862         ::CloseHandle(hUtf8NoBOMFile);

 863         return FALSE;

 864     }

 865     ZeroMemory(lpWriteContentByUTF8, cbWriteContentByUTF8);

 866 

 867     lpWriteContentByUTF8[0] = 0xef;

 868     lpWriteContentByUTF8[1] = 0xbb;

 869     lpWriteContentByUTF8[2] = 0xbf;

 870     CopyMemory(lpWriteContentByUTF8 + UTF8_SIGN, lpReadContentByUTF8NoBOM, cbWriteContentByUTF8 - UTF8_SIGN);

 871 

 872     hUtf8File = ::CreateFile(lpUtf8File, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 873     if (INVALID_HANDLE_VALUE == hUtf8File)

 874     {

 875         printf("Terminal failure: Unable to write to file.\n");

 876         free(lpWriteContentByUTF8);

 877         free(lpReadContentByUTF8NoBOM);

 878         ::CloseHandle(hUtf8NoBOMFile);

 879         return FALSE;

 880     }

 881     SetFilePointer(hUtf8File, 0, NULL, FILE_BEGIN);

 882     if (FALSE == WriteFile(hUtf8File, lpWriteContentByUTF8, cbWriteContentByUTF8, &cbPreWriteContentByUTF8, NULL))

 883     {

 884         int errLogNumber = GetLastError();

 885         printf_s("error number:%d\n", errLogNumber);

 886         free(lpWriteContentByUTF8);

 887         free(lpReadContentByUTF8NoBOM);

 888         ::CloseHandle(hUtf8NoBOMFile);

 889         ::CloseHandle(hUtf8File);

 890         return FALSE;

 891     }

 892 

 893 

 894     //release resource

 895     free(lpWriteContentByUTF8);

 896     free(lpReadContentByUTF8NoBOM);

 897     ::CloseHandle(hUtf8NoBOMFile);

 898     ::CloseHandle(hUtf8File);

 899 

 900     return TRUE;

 901 

 902 }

 903 

 904 BOOL Utf16leFileToUtf16beFile(CONST LPTSTR lpUtf16leFile, CONST LPTSTR lpUtf16beFile)

 905 {

 906     HANDLE hUtf16leFile = NULL;

 907     HANDLE hUtf16beFile = NULL;

 908 

 909 

 910     //create file

 911     hUtf16leFile = ::CreateFile(lpUtf16leFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 912     if (INVALID_HANDLE_VALUE == hUtf16leFile)

 913     {

 914         int errLogNumber = GetLastError();

 915         printf_s("error number:%d\n", errLogNumber);

 916         return FALSE;

 917     }

 918 

 919 

 920     //read UTF16le encode file content

 921     LPBYTE lpReadContentByUTF16le = NULL;

 922     DWORD cbReadContentByUTF16le = 0;

 923     DWORD cbPreReadContentByUTF16le = 0;

 924     DWORD cchReadContentByUTF16le = 0;

 925 

 926     cbReadContentByUTF16le = SetFilePointer(hUtf16leFile, 0, NULL, FILE_END);

 927     if (INVALID_SET_FILE_POINTER == cbReadContentByUTF16le)

 928     {

 929         int errLogNumber = GetLastError();

 930         printf_s("error number:%d\n", errLogNumber);

 931         ::CloseHandle(hUtf16leFile);

 932         return FALSE;

 933     }

 934     if (0 != cbReadContentByUTF16le % 2)

 935     {

 936         printf_s("read byte error\n");

 937         ::CloseHandle(hUtf16leFile);

 938         return FALSE;

 939     }

 940     lpReadContentByUTF16le = (BYTE *)malloc(cbReadContentByUTF16le);

 941     if (NULL == lpReadContentByUTF16le)

 942     {

 943         printf_s("malloc error\n");

 944         ::CloseHandle(hUtf16leFile);

 945         return FALSE;

 946     }

 947     ZeroMemory(lpReadContentByUTF16le, cbReadContentByUTF16le);

 948     SetFilePointer(hUtf16leFile, 0, NULL, FILE_BEGIN);

 949     if (FALSE == ReadFile(hUtf16leFile, lpReadContentByUTF16le, cbReadContentByUTF16le, &cbPreReadContentByUTF16le, NULL))

 950     {

 951         int errLogNumber = GetLastError();

 952         printf_s("error number:%d\n", errLogNumber);

 953         free(lpReadContentByUTF16le);

 954         ::CloseHandle(hUtf16leFile);

 955         return FALSE;

 956     }

 957     //std::wstring wstrText(lpReadContentByUTF16le);

 958     //printf("%s\n", wstrText);

 959     if (sizeof(WCHAR) != sizeof(BYTE)*2)

 960     {

 961         free(lpReadContentByUTF16le);

 962         ::CloseHandle(hUtf16leFile);

 963         return FALSE;

 964     }

 965 

 966 

 967     //write UTF8NoBOM encode file content

 968     LPBYTE lpWriteContentByUTF16be = NULL;

 969     DWORD cbWriteContentByUTF16be = 0;

 970     DWORD cbPreWriteContentByUTF16be = 0;

 971     DWORD cchWriteContentByUTF16be = 0;

 972 

 973     cbWriteContentByUTF16be = cbReadContentByUTF16le;

 974     lpWriteContentByUTF16be = (BYTE *)malloc(cbWriteContentByUTF16be);

 975     if (NULL == lpWriteContentByUTF16be)

 976     {

 977         printf_s("malloc error\n");

 978         free(lpReadContentByUTF16le);

 979         ::CloseHandle(hUtf16leFile);

 980         return FALSE;

 981     }

 982     ZeroMemory(lpWriteContentByUTF16be, cbWriteContentByUTF16be);

 983 

 984     CopyMemory(lpWriteContentByUTF16be, lpReadContentByUTF16le, cbWriteContentByUTF16be);

 985 

 986     for (DWORD i = 0; i < cbWriteContentByUTF16be; i += 2)//每两值交换

 987     {

 988         lpWriteContentByUTF16be[i] = lpWriteContentByUTF16be[i] ^ lpWriteContentByUTF16be[i + 1];

 989         lpWriteContentByUTF16be[i + 1] = lpWriteContentByUTF16be[i + 1] ^ lpWriteContentByUTF16be[i];

 990         lpWriteContentByUTF16be[i] = lpWriteContentByUTF16be[i] ^ lpWriteContentByUTF16be[i + 1];

 991         //BYTE hex_ = 0x0;

 992         //hex_ = lpWriteContentByUTF16be[i];

 993         ////printf("%x\n", lpWriteContentByUTF16be[i]);

 994         //lpWriteContentByUTF16be[i] = lpWriteContentByUTF16be[i + 1];

 995         //lpWriteContentByUTF16be[i + 1] = hex_;

 996     }

 997 

 998     hUtf16beFile = ::CreateFile(lpUtf16beFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 999     if (INVALID_HANDLE_VALUE == hUtf16beFile)

1000     {

1001         printf("Terminal failure: Unable to write to file.\n");

1002         free(lpWriteContentByUTF16be);

1003         free(lpReadContentByUTF16le);

1004         ::CloseHandle(hUtf16leFile);

1005         return FALSE;

1006     }

1007     SetFilePointer(hUtf16beFile, 0, NULL, FILE_BEGIN);

1008     if (FALSE == WriteFile(hUtf16beFile, lpWriteContentByUTF16be, cbWriteContentByUTF16be, &cbPreWriteContentByUTF16be, NULL))

1009     {

1010         int errLogNumber = GetLastError();

1011         printf_s("error number:%d\n", errLogNumber);

1012         free(lpWriteContentByUTF16be);

1013         free(lpReadContentByUTF16le);

1014         ::CloseHandle(hUtf16leFile);

1015         ::CloseHandle(hUtf16beFile);

1016         return FALSE;

1017     }

1018 

1019 

1020     //release resource

1021     free(lpWriteContentByUTF16be);

1022     free(lpReadContentByUTF16le);

1023     ::CloseHandle(hUtf16leFile);

1024     ::CloseHandle(hUtf16beFile);

1025 

1026     return TRUE;

1027 

1028 }

1029 

1030 BOOL Utf16beFileToUtf16leFile(CONST LPTSTR lpUtf16beFile, CONST LPTSTR lpUtf16leFile)

1031 {

1032     return Utf16leFileToUtf16beFile(lpUtf16beFile, lpUtf16leFile);

1033 }

1034 

1035 

1036 

1037 

1038 enum FileCodeType

1039 {

1040     OTHER = 0,

1041     UTF16LE,

1042     UTF8,

1043     UTF8_NO_BOM,

1044     UTF16BE

1045 };

1046 

1047 //************************************

1048 // Method:    CodeFileAToCodeFileB

1049 // FullName:  CodeFileAToCodeFileB

1050 // Access:    public 

1051 // Returns:   bool

1052 // Qualifier:读取指定编码文件A的内容输出到指定编码文件B

1053 // Parameter: CONST LPTSTR lpFileA:输入文件

1054 // Parameter: CONST FileCodeType wCodeTypeA:输入文件类型(包括:utf16le\utf16be\utf8\ANSI)

1055 // Parameter: CONST LPTSTR lpFileB:输出文件

1056 // Parameter: CONST FileCodeType wCodeTypeB:输出文件类型(包括:utf16le\utf16be\utf8\utf8 without BOM\ANSI)

1057 //************************************

1058 BOOL CodeFileAToCodeFileB(CONST LPTSTR lpFileA, CONST FileCodeType emCodeTypeA, CONST LPTSTR lpFileB, CONST FileCodeType emCodeTypeB)

1059 {

1060     BOOL bSUCCESS = FALSE;

1061     HANDLE hFileA = NULL;

1062     HANDLE hFileB = NULL;

1063 

1064     if (OTHER == emCodeTypeA || OTHER == emCodeTypeB)

1065     {

1066         return bSUCCESS;

1067     }

1068     if (NULL == lpFileA || NULL == lpFileB)

1069     {

1070         return bSUCCESS;

1071     }

1072 

1073     switch (emCodeTypeA)

1074     {

1075         case UTF16LE:

1076         {

1077             switch (emCodeTypeB)

1078             {

1079                 case UTF16BE:

1080                 {

1081                     return Utf16leFileToUtf16beFile(lpFileA, lpFileB);

1082                 }

1083                     break;

1084                 case UTF8:

1085                 {

1086                     return Utf16leFileToUtf8File(lpFileA, lpFileB);

1087                 }

1088                     break;

1089                 case UTF8_NO_BOM:

1090                 {

1091                     return Utf16leFileToUtf8NoBOMFile2(lpFileA, lpFileB);

1092                 }

1093                     break;

1094                 default:;

1095             }

1096         }

1097             break;

1098         case UTF8:

1099         {

1100             switch (emCodeTypeB)

1101             {

1102                 case UTF16LE:

1103                 {

1104                     return Utf8FileToUtf16leFile(lpFileA, lpFileB);

1105                 }

1106                 break;

1107                 case UTF16BE:

1108                 {

1109 

1110                 }

1111                 break;

1112                 case UTF8_NO_BOM:

1113                 {

1114                     return Utf8FileToUtf8NoBOMFile(lpFileA, lpFileB);

1115                 }

1116                 break;

1117                 default:;

1118             }

1119         }

1120             break;

1121         case UTF8_NO_BOM:

1122         {

1123             switch (emCodeTypeB)

1124             {

1125                 case UTF16LE:

1126                 {

1127                     return Utf8NoBOMFileToUtf16leFile(lpFileA, lpFileB);

1128                 }

1129                 break;

1130                 case UTF8:

1131                 {

1132                     return Utf8NoBOMFileToUtf8File(lpFileA, lpFileB);

1133                 }

1134                 break;

1135                 case UTF16BE:

1136                 {

1137 

1138                 }

1139                 break;

1140                 default:;

1141             }

1142         }

1143             break;

1144         case UTF16BE:

1145         {

1146             switch (emCodeTypeB)

1147             {

1148                 case UTF16LE:

1149                 {

1150                     return Utf16beFileToUtf16leFile(lpFileA, lpFileB);

1151                 }

1152                 break;

1153                 case UTF8:

1154                 {

1155                     

1156                 }

1157                 break;

1158                 case UTF8_NO_BOM:

1159                 {

1160 

1161                 }

1162                 break;

1163                 default:;

1164             }

1165         }

1166         break;

1167         default:;

1168     }

1169 

1170     return bSUCCESS = TRUE;

1171 }

1172 

1173 

1174 

1175 

1176 void test__BYTETOCHAR();

1177 void test__BYTETOWCHAR();

1178 

1179 int _tmain(int argc, _TCHAR* argv[])

1180 {

1181 

1182 

1183 

1184     CONST LPTSTR lpInputFileUTF8 = TEXT("input-utf8.txt");

1185     CONST LPTSTR lpInputFileUTF16le = TEXT("input-utf16le.txt");

1186     CONST LPTSTR lpInputFileUTF16be = TEXT("input-utf16be.txt");

1187     CONST LPTSTR lpInputFileUTF8NoBOM = TEXT("input-utf8-no-bom.txt");

1188 

1189     CONST LPTSTR lpOutputFileUTF8NoBOM = TEXT("output-utf8-no-bom.txt");

1190     CONST LPTSTR lpOutputFileUTF8 = TEXT("output-utf8.txt");

1191     CONST LPTSTR lpOutputFileUTF16le = TEXT("output-utf16le.txt");

1192     CONST LPTSTR lpOutputFileUTF16be = TEXT("output-utf16be.txt");

1193 

1194 

1195     //CodeFileAToCodeFileB(lpInputFileUTF16le, UTF16LE, lpOutputFileUTF8, UTF8);

1196     //CodeFileAToCodeFileB(lpInputFileUTF16le, UTF16LE, lpOutputFileUTF8NoBOM, UTF8_NO_BOM);

1197     //CodeFileAToCodeFileB(lpInputFileUTF8, UTF8, lpOutputFileUTF8NoBOM, UTF8_NO_BOM);

1198     //CodeFileAToCodeFileB(lpInputFileUTF8, UTF8, lpOutputFileUTF16le, UTF16LE);

1199     //CodeFileAToCodeFileB(lpInputFileUTF8NoBOM, UTF8_NO_BOM, lpOutputFileUTF16le, UTF16LE);

1200     //CodeFileAToCodeFileB(lpInputFileUTF8NoBOM, UTF8_NO_BOM, lpOutputFileUTF8, UTF8);

1201     //CodeFileAToCodeFileB(lpInputFileUTF16le, UTF16LE, lpOutputFileUTF16be, UTF16BE);

1202     //CodeFileAToCodeFileB(lpInputFileUTF16be, UTF16BE, lpOutputFileUTF16le, UTF16LE);

1203 

1204 

1205 

1206 

1207     //test__BYTETOCHAR();

1208     test__BYTETOWCHAR();

1209 

1210 

1211     return 0;

1212 }

1213 

1214 

1215 

1216 

1217 

1218 void test__BYTETOCHAR()

1219 {

1220     //TEXT("output to utf8 with BOM.\r\n输出到utf8带BOM的文件中。\r\nengli  sh");

1221     HANDLE hFileA = NULL;

1222     HANDLE hFileB = NULL;

1223     LPBYTE lpByte = NULL;

1224     INT cbByte = 0;

1225     INT cchByte = 0;

1226     LPSTR lpChar = NULL;

1227     INT cbChar = 0;

1228     INT cchChar = 0;

1229 

1230     hFileA = ::CreateFile(TEXT("input.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

1231     if (INVALID_HANDLE_VALUE == hFileA) return;

1232     cchByte = cbByte = SetFilePointer(hFileA, 0, NULL, FILE_END);

1233     if (INVALID_SET_FILE_POINTER  == cbByte) return;

1234     lpByte = (BYTE *)malloc(cbByte);

1235     if (NULL == lpByte) return;

1236     ZeroMemory(lpByte, cbByte);

1237     SetFilePointer(hFileA, 0, NULL, FILE_BEGIN);

1238     if (FALSE == ReadFile(hFileA, lpByte, cbByte, NULL, NULL)) return;

1239 

1240     cchChar = cbChar = cbByte;

1241     lpChar = (CHAR *)malloc(cbChar);

1242     if (NULL == lpChar) return;

1243     ZeroMemory(lpChar, cbChar);

1244 

1245     for (INT i = 0; i != cbByte; ++i)

1246     {

1247         lpChar[i] = (CHAR)lpByte[i];

1248     }

1249 

1250     //////////////////////////////////////////////////////////////////////////

1251 

1252     std::string strText(lpChar);

1253     printf("%s\n", strText.c_str());

1254 

1255     //////////////////////////////////////////////////////////////////////////

1256 

1257     hFileB = ::CreateFile(TEXT("output.txt"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

1258     SetFilePointer(hFileB, 0, NULL, FILE_BEGIN);

1259     if (FALSE == WriteFile(hFileB, lpChar, cbChar, NULL, NULL)) return;

1260 

1261     free(lpByte);

1262     free(lpChar);

1263     CloseHandle(hFileA);

1264     CloseHandle(hFileB);

1265 }

1266 void test__BYTETOWCHAR()

1267 {

1268     HANDLE hFileA = NULL;

1269     HANDLE hFileB = NULL;

1270     LPBYTE lpByte = NULL;

1271     INT cbByte = 0;

1272     INT cchByte = 0;

1273     LPWSTR lpWChar = NULL;

1274     INT cbWChar = 0;

1275     INT cchWChar = 0;

1276 

1277     hFileA = ::CreateFile(TEXT("input.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

1278     if (INVALID_HANDLE_VALUE == hFileA) return;

1279     cchByte = cbByte = SetFilePointer(hFileA, 0, NULL, FILE_END);

1280     if (INVALID_SET_FILE_POINTER == cbByte) return;

1281     lpByte = (BYTE *)malloc(cbByte);

1282     if (NULL == lpByte) return;

1283     ZeroMemory(lpByte, cbByte);

1284     SetFilePointer(hFileA, 0, NULL, FILE_BEGIN);

1285     if (FALSE == ReadFile(hFileA, lpByte, cbByte, NULL, NULL)) return;

1286 

1287     cbWChar = cbByte;

1288     cchWChar = cbWChar / sizeof(WCHAR);

1289     lpWChar = (WCHAR *)malloc(cbWChar);

1290     if (NULL == lpWChar) return;

1291     ZeroMemory(lpWChar, cbWChar);

1292 

1293     CopyMemory(lpWChar, lpByte, cbWChar);

1294 

1295     //////////////////////////////////////////////////////////////////////////

1296 

1297     std::wstring strText(lpWChar);

1298     wprintf(L"%s\n", strText.c_str());

1299 

1300     //////////////////////////////////////////////////////////////////////////

1301 

1302     hFileB = ::CreateFile(TEXT("output.txt"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

1303     SetFilePointer(hFileB, 0, NULL, FILE_BEGIN);

1304     if (FALSE == WriteFile(hFileB, lpWChar, cbWChar, NULL, NULL)) return;

1305 

1306     free(lpByte);

1307     free(lpWChar);

1308     CloseHandle(hFileA);

1309     CloseHandle(hFileB);

1310 }

1311 void test_CHARTOBYTE()

1312 {

1313 

1314 }

1315 void test__WCHARTOBYTE()

1316 {

1317 

1318 }

*注:由于较长建议大家放到编译器里面阅读。具体编码实例放到这里

 

你可能感兴趣的:(编码格式)