字符UTF16 转 UTF8 问题

h2plus0 2015-05-04 06:25:09
大家好,
请问为什么下面程序, 出现 core dump
另外,请问iconv第2,4个参数为什么是char**类型, 而不是char*类型?
谢谢!

程序输出:
test1: gconv.c:74: __gconv: Assertion `outbuf != ((void *)0) && *outbuf != ((void *)0)' failed.
Aborted (core dumped)


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

char* utf16ToUtf8(const wchar_t* ws, size_t wcharCount) {
static char buf[10001];

size_t dstlen = 10000;
iconv_t conv = iconv_open("UTF8", "UTF16");
if(conv==(iconv_t)-1) {
throw "utf16ToUtf8 conv -1";
}
size_t srcLen = wcharCount*2;
iconv(conv, (char**)&ws, &srcLen, (char**)&buf, &dstlen);
iconv_close(conv);
return buf;
}

void testUtf16ToUtf8() {
char data[]={0x00, 0x63, 0x00, 0x6f}; // "co" 的UTF16 字符串
//char data[]={0x63, 0x00, 0x6f, 0x00};
char* s = utf16ToUtf8((wchar_t*)data, 2);
printf("s:%s \n", s);
}

// 运行环境:ubuntu, 14.04
int main() {
try {
testUtf16ToUtf8();
return 0;
}
catch(...) {
printf("ERROR \n");
}
return -1;
}

...全文
379 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
h2plus0 2015-05-04
  • 打赏
  • 举报
回复
原来ubuntu下,wchar_t 缺省大小是4, 需要制定g++参数: -fshort-wchar
h2plus0 2015-05-04
  • 打赏
  • 举报
回复
引用 3 楼 u011004037 的回复:
额,好像有错了。。。

char *outbuf = buf;
iconv(conv, (char**)&ws, &srcLen, &outbuf , &dstlen);
谢谢! 根据您的指点, 我修改了代码, 但还是有点问题 (如果字符串有中文, 只能转换1个字符) 程序输出: iconv ret:0 s:中

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

char* utf16ToUtf8(const wchar_t* ws, size_t wcharCount) {
	static char buf[10001];
	size_t dstlen = 10000;

	iconv_t conv = iconv_open("UTF8", "UTF16");
	if(conv==(iconv_t)-1) {
		throw "utf16ToUtf8 conv -1";
	}
	size_t srcLen = wcharCount*2;

	/*
	from man: The inbuf argument is the address of a variable that points to the first character of the input sequence;
	所以,为防万一,也声明一个变量保存ws
	*/
	char* src = (char*)ws;
	char* outbuf = buf;
	int ret = iconv(conv, (char**)&src, &srcLen, &outbuf , &dstlen);
	printf("iconv ret:%d \n", ret);
	iconv_close(conv);
	return buf;
}

void testUtf16ToUtf8() {
	//char data[]={0x00, 0x63, 0x00, 0x6f}; // "co" 的UTF16 字符串
	//char data[]={0x63, 0x00, 0x6f, 0x00};
	wchar_t data[] = L"中文12356";
	char* s = utf16ToUtf8((wchar_t*)data, 7);
	printf("s:%s \n", s);
}

//#include <locale.h>
// 运行环境:ubuntu, 14.04
int main() {
	try {
		//setlocale(LC_ALL, "C");
		testUtf16ToUtf8();
		return 0;
	}
	catch(const char* e) {
		printf("ERROR:%s \n", e);
	}
	catch(...) {
		printf("ERROR ");
	}
	return -1;
}
youzi05 2015-05-04
  • 打赏
  • 举报
回复
额,好像有错了。。。

char *outbuf = buf;
iconv(conv, (char**)&ws, &srcLen, &outbuf , &dstlen);
youzi05 2015-05-04
  • 打赏
  • 举报
回复
抱歉写错了,应该是这个

char **outbuf = (char**)&buf;
iconv(conv, (char**)&ws, &srcLen, outbuf , &dstlen);
youzi05 2015-05-04
  • 打赏
  • 举报
回复
这个,我没用过这个函数,说说我刚才搜索到的信息吧, icon函数需要修改传进去的参数,所以第二个和第四个是char**,第三个和第五个是int* 问题应该就是出在这,因为你的buf是不可变的。当这个函数进行 (*outbuf)++ 这类运算时应该会报错 可以试试这个 char **outbuf = (char**)&buf; iconv(conv, (char**)&ws, &srcLen, (char**)&outbuf , &dstlen);
引用
iconv函数进行实际的转换,需要给出两个间接缓冲区指针和剩余字节数指针。该函数需要更新所有相关信息,因此将不可改写的指针传递给iconv是错误的。
参考网页: http://blog.csdn.net/hongchangfirst/article/details/8951391 http://baike.baidu.com/link?url=lKKf4bD8FfdIIpzcUVgVYjcqXnR0HkgNWzSdJaisD8sVxire4cnC0Ai0cBFafLubqy_Wwb7TGJQOZWG9e6zLR_
h2plus0 2015-05-04
  • 打赏
  • 举报
回复
引用 6 楼 buyong 的回复:
char* s s没有分配空间呀!!
谢谢, s 指向的是utf16ToUtf8()里面的static buf. 如果在 g++ 里面指定 -fshort-wchar 参数, 4楼的程序 可以正确得到utf8字符串
buyong 2015-05-04
  • 打赏
  • 举报
回复
char* s s没有分配空间呀!!

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧