How to Use libiconv Character Set conversion Library

Source: Internet
Author: User

Character Set conversion is inevitable in software development. Many conversion methods, such as writing character set conversion libraries and using
Provided by Windows
Multibytetowidechar function; it takes a long time to compile the function.
Multibytetowidechar and other functions are inconvenient for cross-platform value shifting. Therefore, this article describes how to use
Linux and
Available in Windows
The libiconv library converts character sets.

We can
Http://www.gnu.org/software/libiconv/download the latest
Libiconv;
The installation and use of libiconv-1.9.2-1.exe as an example;
Libiconv installation is very convenient, just like most
Windows application installation is the same as direct
Step by step. During the installation process, you will be prompted whether to download the source code. The source code is not very useful to us, so you will not download the source code. After installation
In the installation directory of libiconv
The man/pdf directory contains
Libiconv-man.pdf, this file is
Libiconv functions.
If you are not familiar with libiconv, please take a look at this file.

To
Used in vc6
Libiconv library we must
Vc6.
Select
Tools à
Options..., select
On the directories tab
Select
Include files, at the bottom of
Directories: add
In the libiconv installation directory
Include directory, such
1-1:


Figure
1-1
In this way, we have configured
Iconv. h file to find the Directory; because
Use libiconv
Libiconv. Lib, so we should configure it later.
The Directory of the libiconv. Lib file.
1-1
Show directories for, select
Library files, at the bottom of
Directories: Add to the list
In the libiconv installation directory
Lib directory;
Click OK to complete configuration
Click OK.
Under the libiconv installation directory
In the bin directory
Copy libiconv2.dll TO THE SYSTEM
In the System32 directory.

The conversion example is as follows:

# Include <stdio. h>
# Include <string. h>
// Package letter
Libiconv library header file

# Include <iconv. h>
// Import
Libiconv Library

# Pragma comment (Lib, "libiconv. lib ")
 
Int changecode (const char * pfromcode,
Const char * ptocode,
Const char * pinbuf,
Size_t * iinlen,
Char * poutbuf,
Size_t * ioutlen );
 
Int main (INT argc, char * argv [])
{
Char sinbuf [100];
Char soutbuf [100];
Size_t iinlen = 0;
Size_t ioutlen = 100;
Int iret;
Strcpy (sinbuf, "Test
Test source ");
Memset (soutbuf, 0x00,100 );
Iinlen = strlen (sinbuf );
Iret = changecode ("GBK", "UTF-16", sinbuf, & iinlen, soutbuf, & ioutlen );
Return 0;
}
 
Int changecode (const char * pfromcode,
Const char * ptocode,
Const char * pinbuf,
Size_t * iinlen,
Char * poutbuf,
Size_t * ioutlen)
{
Int iret;
// Enable character set Conversion

Iconv_t hiconv = iconv_open (ptocode, pfromcode );
If (-1 = (INT) hiconv)
{
Return-1; // the character set that fails to be opened. It may not be supported.

}
// Start Conversion

Iret = iconv (hiconv, (const char **) (& pinbuf), iinlen, (char **) (& poutbuf), ioutlen );
// Disable Character Set Conversion

Iconv_close (hiconv );
Return iret;
}
Character Set conversion is so simple; of course, if you are interested
Install it in Linux.
Libiconv and put the above Code
Linux
Compile GCC.

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.