1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include "iconv.h"
void print_hex(char *str)
{
printf("%.2x", (unsigned char) *str);
str++;
while (*str) {
printf(" %.2x", (unsigned char) *str);
str++;
}
}
void convert_string(iconv_t cd, char *str)
{
char outbuf[256];
char *outptr = outbuf;
const char *inptr = str;
size_t outsize = sizeof(outbuf);
size_t insize = strlen(str);
int ret, err = 0;
memset(outbuf, 0, sizeof(outbuf));
print_hex(str);
printf(" -> ");
ret = iconv(cd, &inptr, &insize, &outptr, &outsize);
if (ret < 0)
err= errno;
print_hex(outbuf);
if (err)
printf(" (%s)", strerror(err));
printf("\n");
}
void convert_args(char *from, char *to, int argc, char **argv)
{
iconv_t cd;
int i;
printf("\n>>> %s: %s -> %s\n", basename(argv[0]), from, to);
cd = iconv_open(to, from);
if (cd < 0) {
printf("iconv_open(\"%s\", \"%s\"): %s\n", to, from, strerror(errno));
return;
}
for (i = 1; i < argc; i++)
convert_string(cd, argv[i]);
iconv_close(cd);
}
int main(int argc, char **argv)
{
char *codesets[] = { "ASCII", "ISO-8859-1", "iso8859-1", "UTF-8", "utf8", "invalid", NULL };
char **from, **to;
for (from = codesets; *from; from++)
for (to = codesets; *to; to++)
convert_args(*from, *to, argc, argv);
return 0;
}
|