Use API as a simple body conversion

zhaozj2021-02-08  202

Time: 2004/3/25 Author: Robert Reference: MSDN Email: zsc771120@yahoo.com.cn Keywords: simplified and traditional API internal code BIG5 GB2312 MultiByteToWideChar WideCharToMultiByte purpose: to help a friend troubled by the code conversion

1. Enter a BIG5 character, return to GB Simplified characters

/ / -------------------------------------------------------------------------------------------- ---------------------------

// Function Enter BIG5 characters, return to GB Simplified characters

/ / -------------------------------------------------------------------------------------------- ---------------------------

ANSISTRING __FASTCALL BIG2GB (ANSISTRING SBIG)

{

Char * pszbig5 = null; // BIG5 encoded characters

Wchar_t * wszunicode = null; // unicode encoded characters

Char * pszgbt = null; // GB encoded Traditional characters

Char * pszgbs = null; // GB encoded Simplified characters

Ansistring sgb; // Returned string

INT ilen = 0; // Number of characters that need to be converted

pszbig5 = sbig.c_str (); // Read character parameters required to convert

// Calculate the number of characters

Ilen = MultibyTowideChar (950, 0, PSZBIG5, -1, NULL, 0);

// Assign memory for WSZUnicode

WSZUNICODE = New Wchar_T [Ilen 1];

/ / Convert BIG5 code to Unicode code, use API functions MultibyToWideChar

MultibyTetowideChar (950, 0, PSZBIG5, -1, WSZUNICODE, ILEN);

// Calculate the number of characters

Ilen = Widechartomultibyte (936, 0, (Pwstr) WSZUNICODE, -1, NULL, 0, NULL, NULL

/ / Assign memory for PSZGBT

Pszgbt = new char [Ilen 1];

/ / Assign memory for PSZGBS

PSZGBS = New Char [Ilen 1];

/ / Convert Unicode code to GB code Traditional, use API functions WideChartomultibyte

Widechartomultibyte (936, 0, (PWSTR) WSZUNICODE, -1, PSZGBT, ILEN, NULL, NULL

/ / Convert GB code Traditional to GB code Simplified, use API functions LCMapString

LcMapString (0x0804, lcmap_simplified_chinese, pszgbt, -1, pszgbs, ilen);

/ / Return to GB code Simplified characters

SGB ​​= PSZGBS;

/ / Release memory

delete [] WSZUNICODE;

Delete [] pszgbt;

PSZGBS;

Return SGB;

}

2. Enter the GB character, return to BIG5 characters

/ / -------------------------------------------------------------------------------------------- ---------------------------

// Function Enter GB Character, return BIG5 characters

/ / -------------------------------------------------------------------------------------------- ---------------------------

Ansistring __fastcall GB2BIG (ANSISUSTRING SGB)

{

Char * pszgbt = null; // GB encoded Traditional Char * pszgbs = NULL; // GB encoded Simplified characters

Wchar_t * wszunicode = null; // unicode encoded characters

Char * pszbig5 = null; // BIG5 encoded characters

Ansistring sbig5; // Returned string

INT ilen = 0; // Number of characters that need to be converted

PSZGBS = sgb.c_str (); // Read the character parameters required to convert

// Calculate the number of characters

Ilen = MultibyToWideChar (936, 0, PSZGBS, -1, NULL, 0);

/ / Assign memory for PSZGBT

Pszgbt = new char [Ilen * 2 1];

/ / Convert GB code Simplified to GB code Traditional, use API functions LCMapString

LcMapString (0x0804, lcmap_traditional_chinese, pszgbs, -1, pszgbt, ilen * 2);

// Assign memory for WSZUnicode

WSZUNICODE = New Wchar_T [Ilen 1];

/ / Convert GB code to Unicode code, use API functions multibytetowideChar

MultibyToWideChar (936, 0, PSZGBT, -1, WSZUNICODE, ILEN);

// Calculate the number of characters

Ilen = Widechartomultibyte (950, 0, (pwstr) WSZUNICODE, -1, NULL, 0, NULL, NULL

/ / Distribute memory for PSZBIG5

Pszbig5 = new char [Ilen 1];

/ / Convert Unicode code to the BIG5 code, use the API function WideChartomultibyte

Widechartomultibyte (950, 0, (PWSTR) WSZUNICODE, -1, PSZBIG5, ILEN, NULL, NULL;

// Return to BIG5 code characters

SBIG5 = PSZBIG5;

/ / Release memory

delete [] WSZUNICODE;

Delete [] pszgbt;

delete [] pszbig5;

Return sbig5;

}

转载请注明原文地址:https://www.9cbs.com/read-3051.html

New Post(0)