123456789101112131415161718 | /* typedefvoid /* codeset TOCODE. This function is a possible cancellation points and therefore not marked with __THROW. */ externiconv_ticonv_open /* code conversion algorithm specified by CD and place up to *OUTBYTESLEFT bytes in buffer at *OUTBUF. */ externsize_ticonv char **__restrict __inbuf, size_t *__restrict __inbytesleft, char **__restrict __outbuf, size_t *__restrict __outbytesleft); /* This function is a possible cancellation points and therefore not marked with __THROW. */ externinticonv_close |
1234567 | #ifndef #define #endif #if typedef __SIZE_TYPE__ size_t ; #ifdef typedef long ssize_t; |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | #include #include #include #include #include #include #define void dumprawmsg( char *p, int len) { int i = 0; for (i = 0; i < len; i++) { unsigned char c = p[i]; printf ( "%.2X " , c); } printf ( "\n" ); } int convmsg( char * src, char * des, int srclen, int deslen, const char *srctype, const char *destype) { if ( strcmp (srctype, destype) == 0) { memcpy (des, src, MIN(srclen, deslen)); return 0; } iconv_t conv = iconv_open (destype, srctype); if (conv == (iconv_t)-1) { printf ( "iconvopen err\n" ); return -1; } char *in = src; char *out = des; // // // // // // // // // // // // // // // // // // // size_t avail = deslen; size_t insize = srclen; char *wrptr = des; char *inptr = src; while (avail > 0) { size_t nread; size_t nconv; printf ( "avail:%d\n" , avail); /* Do the conversion. */ nconv = iconv (conv, &inptr, &insize, &wrptr, &avail); if (nconv == ( size_t ) -1) { /* Not everything went right. It might only be an unfinished byte sequence at the end of the buffer. Or it is a real problem. */ if ( errno == EINVAL) { /* This is harmless. Simply move the unused bytes to the beginning of the buffer so that they can be used in the next round. */ //memmove (inbuf, inptr, insize); printf ( "EINVAL\n" ); } else { /* It is a real problem. Maybe we ran out of space in the output buffer or we have invalid input. In any case back the file pointer to the position of the last processed byte. */ printf ( "error\n" ); break ; } } } iconv_close (conv); return 0; } int main( int argc, char * argv[]) { if (argc < 3) { printf ( "need two type para\n" ); return -1; } printf ( "type in %s\n, type out %s\n" , argv[1], argv[2]); char src[100] = "abcd 1234 其他" ; char des[100] = {0}; int srclen = 50; int deslen = 50; const char * srctype = argv[1]; const char * destype = argv[2]; dumprawmsg(des, 400); int ret = convmsg(src, des, srclen, deslen, srctype, destype); dumprawmsg(des, 400); printf ( "des is : %s\n" , des); return 0; } |