Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Convert Shift-JIS to utf-8 and back.
Visual C++ source code showing how to convert Shift-JIS character data to utf-8 and the reverse (converting utf-8 to Shift-JIS). #define _CRTDBG_MAP_ALLOC
#include "stdafx.h"
#include <stdio.h>
#include <crtdbg.h>
#include "C:/ck2000/components/ChilkatLib/Package/include/CkString.h"
#include "C:/ck2000/components/ChilkatLib/Package/include/CkByteData.h"
#include "C:/ck2000/components/ChilkatLib/Package/include/CkCharset.h"
#include "C:/ck2000/components/ChilkatLib/Package/include/CkSettings.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CkCharset *cc = new CkCharset;
cc->UnlockComponent("UnlockCode");
// *************************************************
// Convert from Shift-JIS to utf-8
// *************************************************
// Load the Shift-JIS sample data into memory.
CkByteData *inData = new CkByteData;
inData->loadFile("shiftJisSample.txt");
// Convert from Shift-JIS to utf-8
CkByteData *outData = new CkByteData;
cc->put_ToCharset("utf-8");
cc->put_FromCharset("Shift_JIS");
cc->ConvertData(*inData,*outData);
// Save the output.
outData->saveFile("outUtf8.txt");
// *************************************************
// Convert from utf-8 to Shift-JIS
// *************************************************
// Load the utf-8 sample data into memory.
inData->clear();
inData->loadFile("utf8Sample.txt");
// Convert from utf-8 to Shift-JIS
// If there are any utf-8 characters that are not representable in SHIFT-JIS,
// then replace them with a '?' character.
// ErrorAction = 0, non-convertable characters are dropped.
// ErrorAction = 1, non-convertable chars are hexidecimalized to the format xxxxx; where there can be as many as 6 octets in a single utf-8 character.
// ErrorAction = 2, non-convertable chars are replaced with the error string.
// More error actions are available, see http://www.chilkatsoft.com/refdoc
outData->clear();
cc->SetErrorString("?");
cc->put_ErrorAction(2);
cc->put_FromCharset("utf-8");
cc->put_ToCharset("Shift_JIS");
cc->ConvertData(*inData,*outData);
// Save the output.
outData->saveFile("outShiftJIS.txt");
delete inData;
delete outData;
delete cc;
// Only necessary if checking for memory leaks.
// Only call just before exiting the program.
CkSettings::cleanupMemory();
_CrtDumpMemoryLeaks();
return 0;
}
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.