简体   繁体   English

libiconv将字符串从“ ascii”转换为“ utf-8”吗?

[英]libiconv convert string from 'ascii' to 'utf-8'?

I am trying to convert string from 'ascii' to 'utf-8', the 'lencoded_str' function will take a pointer char and return another pointer char. 我正在尝试将字符串从'ascii'转换为'utf-8','lencoded_str'函数将使用一个指针char并返回另一个指针char。 the input pointer char size is unknown, and it will change each time the 'lencoded_str' function called. 输入指针的char大小是未知的,并且每次调用“ lencoded_str”函数时都会改变。 my problem is the 'lencoded_str' function always return nothing. 我的问题是'lencoded_str'函数始终不返回任何内容。

Any help would be much appreciated. 任何帮助将非常感激。

this is just an example 这只是一个例子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iconv.h>

char *lencoded_str(char *in_str){

  iconv_t iconv_obj = iconv_open("utf-8","ascii");
  char *out_str = calloc(strlen(in_str) * 2, sizeof(char));
  char *out_str_start = out_str;

  size_t in_str_bytes_left    = strlen(in_str);  
  size_t out_str_bytes_left   = strlen(in_str) * 2;  
  int iconv_return = iconv(iconv_obj, &in_str,  &in_str_bytes_left, &out_str, &out_str_bytes_left);
  iconv_close(iconv_obj);

  return out_str;
}
int main( ){ 
  printf("out: %s\n", lencoded_str("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"));
  printf("out: %s\n", lencoded_str("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"));
  printf("out: %s\n", lencoded_str("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"));
}

iconv advances toe input and output string pointers so that they point at the end of the converted string. iconv前进脚趾输入和输出字符串指针,以便它们指向转换后的字符串的末尾。

That means you must save the original value of the char buffer and return it. 这意味着您必须保存char缓冲区的原始值并返回它。 You've already had the right idea with out_str_start , but you don't use it: 您已经使用out_str_start有了正确的主意,但是您没有使用它:

return out_str_start;

By using the return value from lencoded_str directly in printf and not storing it, you will leak the memory that yopu calloc ed. 通过直接在printf使用lencoded_str的返回值而不存储它,您将泄漏yopu calloc ed的内存。 You could also go easy on the strlen s; 您也可以在strlen上轻松进行; you only need to call it once. 您只需要调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM