Guest User

Untitled

a guest
Jun 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. // iconv_test.c -*- encoding: utf-8 -*-
  2. // gcc iconv_test.c && (./a.out | nkf -g)
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8.  
  9. #include <iconv.h>
  10.  
  11. #define SIZE 1024
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. char *src, dst[SIZE];
  16. char *p_src, *p_dst;
  17. size_t src_len;
  18. size_t dst_len, dst_len_orig;
  19. size_t result;
  20. iconv_t cd;
  21.  
  22. src = (argv[1] != NULL) ? argv[1] : "こんなテストで大丈夫か?";
  23. p_src = src;
  24. src_len = strlen(src);
  25.  
  26. memset(dst, 0, SIZE);
  27. p_dst = dst;
  28. dst_len = SIZE - 1;
  29. dst_len_orig = dst_len;
  30.  
  31. cd = iconv_open("UTF-32LE", "UTF-8");
  32.  
  33. result = iconv(cd, &p_src, &src_len, &p_dst, &dst_len);
  34. if (result == -1) {
  35. perror("iconv");
  36. exit(-1);
  37. }
  38. *p_dst = '\0';
  39.  
  40. write(STDOUT_FILENO, dst, (dst_len_orig - dst_len));
  41.  
  42. iconv_close(cd);
  43. return 0;
  44. }
Add Comment
Please, Sign In to add comment