Syntax
#include <iconv.h> iconv_t iconv_open(const char *tocode, const char *fromcode);Description
iconv_open performs all the initialization needed to convert characters from the encoded character set specified in the array pointed to by fromcode to the encoded character set specified in the array pointed to by tocode. It creates a conversion descriptor that relates the two encoded character sets. You can then use the conversion descriptor with the iconv function to convert characters between the codesets.
The conversion descriptor remains valid until you close it with iconv_close.
For information about the settings of fromcode, tocode, and their permitted combinations, see the section on internationalization in the VisualAge C++ Programming Guide.
If successful, iconv_open returns a conversion descriptor of type iconv_t. Otherwise, it returns (iconv_t)-1, and sets errno to indicate the error. If you cannot convert between the encoded character sets specified, an error occurs and iconv_open sets errno to EINVAL.
This example shows how to use iconv_open, iconv, and iconv_close to convert characters from one codeset to another.
#include <iconv.h> #include <stdlib.h> #include <stdio.h> int main(void) { const char fromcode[] = "IBM-932"; const char tocode[] = "IBM-930"; iconv_t cd; char in[] = "\x81\x40\x81\x80"; size_t in_size = sizeof(in); char *inptr = in; char out[100]; size_t out_size = sizeof(out); char *outptr = out; int i; if ((iconv_t)(-1) == (cd = iconv_open(tocode, fromcode))) { printf("Failed to iconv_open %s to %s.\n", fromcode, tocode); exit(EXIT_FAILURE); } if ((size_t)(-1) == iconv(cd, &inptr, &in_size, &outptr, &out_size)) { printf("Fail to convert characters to new code set.\n"); exit(EXIT_FAILURE); } *outptr = '\0'; printf("The hex representation of string %s are:\n In codepage %s ==> ", in, fromcode); for (i = 0; in[i] != '\0'; i++) { printf("0x%02x ", in[i]); } printf("\n In codepage %s ==> ", tocode); for (i = 0; out[i] != '\0'; i++) { printf("0x%02x ", out[i]); } if (-1 == iconv_close(cd)) { printf("Fail to iconv_close.\n"); exit(EXIT_FAILURE); } return 0; /**************************************************************************** The output should be similar to : The hex representation of string ₧@₧┘ are: In codepage IBM-932 ==> 0x81 0x40 0x81 0x80 In codepage IBM-930 ==> 0x0e 0x40 0x40 0x44 0x7b 0x0f ****************************************************************************/ }Related Information