Use of libiconv Character Set conversion library in C #

Source: Internet
Author: User

In libiconv Character Set conversion library usage, libiconv can be used to convert different character sets. For example, convert GBK to big5. Find this library as needed in the project. However, this library does not have good support in C. However, since it is a C ++ library, you only need to dynamically load the DLL interface. However, the call is not smooth, and the imported intptr or byte array never gets the data. Later I went back to the C ++ method for calling. After debugging, I finally found the reason.
After the iconv interface completes the conversion, the pointer position is moved back. The pointer returned after calling the DLL in C # is already moved, so the required data cannot be obtained.
After a variety of attempts, there is no way to move the pointer back to the original position.

Later, through the second encapsulation of C ++, the pointer position was moved to the original position in C ++, and then called with C #, finally achieving the goal.

# Include <fstream> // the header file of the libiconv library # include "iconv. H "// import libiconv library # pragma comment (Lib," libiconv. lib ") using namespace STD; # define dll_export extern" C "_ declspec (dllexport) dll_export int changecode (const char * pfromcode, const char * ptocode, const char * pinbuf, optional * iinlen, char * poutbuf, size_t * ioutlen) {size_t outlentemp = * ioutlen; iconv_t hiconv = iconv_open (ptocode, pfromcode); If (-1 = (INT) hiconv) {return-100; // opening failed, character set that may not be supported} // start to convert int iret = iconv (hiconv, (const char **) (& pinbuf), iinlen, (char **) (& poutbuf), ioutlen); If (iret> = 0) {poutbuf = poutbuf-(outlentemp-* ioutlen ); // After conversion, the poutbuf pointer is moved and must be moved back to the starting position} else {iret =-200;} // disable iconv_close (hiconv); Return iret ;}

C # Call part

/// <Summary> /// Character Set conversion. /// you must enable or disable the converter for each conversion. /// </Summary> /// <Param name = "pfromcode"> source character set encoding </param> /// <Param name = "ptocode"> Target character set encoding </param> /// <Param name = "pinbuf"> content to be converted </param> /// <Param name = "iinlen"> length of the content to be converted. After the conversion is successful, it is 0. </param> /// <Param name = "poutbuf"> converted content </param> /// <Param name = "ioutlen"> the conversion length. After the conversion is successful, the original value minus the space occupied by the converted content </param> // <returns> </returns> [dllimport ("charsetconvert. DLL ", callingconvention = callingconvention. cdecl)] public static extern int changecode (string pfromcode, string ptocode, byte [] pinbuf, ref int iinlen, byte [] poutbuf, ref int ioutlen );

 
Private void buttononeconvert_click (Object sender, eventargs e) {string tocode = "big5"; string fromcode = "GBK"; string instr = "K"; byte [] inbuf = encoding. default. getbytes (instr); byte [] outbuf = new byte [100]; int inlen = inbuf. length; int outlen = outbuf. length; int result = charsetconvter. changecode (fromcode, tocode, inbuf, ref inlen, outbuf, ref outlen); If (result <0) {MessageBox. show ("Conversion failed");} else {string outstr = encoding. getencoding ("big5 "). getstring (outbuf); MessageBox. show (outstr );}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.