version ( Windows) import std.c .windows .windows ;
version ( Windows) import std.windows .syserror ;
//version(Posix) import iconv;
import std.utf ;
import std.string ;
import std.stdio ;
extern ( C) export
{
alias void * iconv_t;
size_t iconv( iconv_t cd,
char ** inbuf, size_t * inbytesleft,
char ** outbuf, size_t * outbytesleft) ;
iconv_t iconv_open( const char * tocode, const char * fromcode) ;
int iconv_close( iconv_t cd) ;
}
version ( Windows) string charcterEncodingConvert( const ( char ) [ ] str, uint fromCodePage, uint toCodePage)
{
int result;
wchar [ ] wstr;
char [ ] rstr;
result = MultiByteToWideChar( fromCodePage, 0 , str.ptr , str.length , null , 0 ) ;
wstr.length = result;
result = MultiByteToWideChar( formCodePage, 0 , str.ptr , str.length , wstr.ptr , wstr.length ) ;
if ( result == 0 ) throw new Exception ( "Couldn't convert string: " ~ sysErrorString( GetLastError( ) ) ) ;
result = WideCharToMultiByte( toCodePage, 0 , wstr.ptr , wstr.length , null , 0 , null , null ) ;
rstr.length = result;
result = WideCharToMultiByte( toCodePage, 0 , wstr.ptr , wstr.length , rstr.ptr , rstr.length , null , null ) ;
if ( result == 0 ) throw new Exception ( "Couldn't convert string: " ~ sysErrorString( GetLastError( ) ) ) ;
return cast ( immutable ) rstr;
}
version ( Posix) string charcterEncodingConvert( const ( char ) [ ] str, const ( char ) [ ] fromcode, const ( char ) [ ] tocode)
{
iconv_t cd = iconv_open( toStringz( tocode) , toStringz( fromcode) ) ;
if ( cd is cast ( iconv_t) - 1 ) throw new Exception ( "Couldn't convert string" ) ;
scope ( exit) iconv_close( cd) ;
char [ ] cstr = str.dup ;
char [ ] rstr = new char [ str.length * 2 ] ;
char * inbuf = cstr.ptr ;
char * outbuf = rstr.ptr ;
size_t inleft = cstr.length ;
size_t outleft = rstr.length ;
size_t result = iconv( cd, & inbuf, & inleft, & outbuf, & outleft) ;
if ( result == cast ( size_t ) - 1 ) throw new Exception ( "Couldn't convert string" ) ;
rstr.length = rstr.length - outleft;
return cast ( immutable ) rstr;
}
int main( string [ ] argv)
{
string str = charcterEncodingConvert( "こんにちは" , "UTF-8" , "SHIFT_JIS" ) ;
writeln( str) ;
return 0 ;
}
dmVyc2lvbihXaW5kb3dzKSBpbXBvcnQgc3RkLmMud2luZG93cy53aW5kb3dzOwp2ZXJzaW9uKFdpbmRvd3MpIGltcG9ydCBzdGQud2luZG93cy5zeXNlcnJvcjsKLy92ZXJzaW9uKFBvc2l4KSBpbXBvcnQgaWNvbnY7CmltcG9ydCBzdGQudXRmOwppbXBvcnQgc3RkLnN0cmluZzsKaW1wb3J0IHN0ZC5zdGRpbzsKCmV4dGVybihDKSBleHBvcnQKewoJYWxpYXMgdm9pZCogaWNvbnZfdDsKCQoJc2l6ZV90IGljb252KGljb252X3QgY2QsCgkgICAgICAgICAgICAgY2hhciAqKmluYnVmLCBzaXplX3QgKmluYnl0ZXNsZWZ0LAoJICAgICAgICAgICAgIGNoYXIgKipvdXRidWYsIHNpemVfdCAqb3V0Ynl0ZXNsZWZ0KTsKCWljb252X3QgaWNvbnZfb3Blbihjb25zdCBjaGFyICp0b2NvZGUsIGNvbnN0IGNoYXIgKmZyb21jb2RlKTsKCWludCBpY29udl9jbG9zZShpY29udl90IGNkKTsKfQoKdmVyc2lvbihXaW5kb3dzKSBzdHJpbmcgY2hhcmN0ZXJFbmNvZGluZ0NvbnZlcnQoY29uc3QoY2hhcilbXSBzdHIsIHVpbnQgZnJvbUNvZGVQYWdlLCB1aW50IHRvQ29kZVBhZ2UpCnsKCWludCByZXN1bHQ7Cgl3Y2hhcltdIHdzdHI7CgljaGFyW10gcnN0cjsKCglyZXN1bHQgPSAgTXVsdGlCeXRlVG9XaWRlQ2hhcihmcm9tQ29kZVBhZ2UsIDAsIHN0ci5wdHIsIHN0ci5sZW5ndGgsIG51bGwsIDApOwoJd3N0ci5sZW5ndGggPSByZXN1bHQ7CglyZXN1bHQgPSBNdWx0aUJ5dGVUb1dpZGVDaGFyKGZvcm1Db2RlUGFnZSwgMCwgc3RyLnB0ciwgc3RyLmxlbmd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