gpt4 book ai didi

c - 将 char 转换为 wchar_t 的问题(长度错误)

转载 作者:行者123 更新时间:2023-12-04 06:44:49 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的数据结构,它可以轻松地在 ASCII 字符串和 Unicode 字符串之间来回转换。我的问题是函数 mbstowcs 返回的长度是正确的,但函数 wcslen 在新创建的 wchar_t 字符串上返回的长度不正确。我在这里错过了什么吗?

typedef struct{

wchar_t *string;
long length; // I have also tried int, and size_t
} String;

void setCString(String *obj, char *str){

obj->length = strlen(str);

free(obj->string); // Free original string
obj->string = (wchar_t *)malloc((obj->length + 1) * sizeof(wchar_t)); //Allocate space for new string to be copied to

//memset(obj->string,'\0',(obj->length + 1)); NOTE: I tried this but it doesn't make any difference

size_t length = 0;

length = mbstowcs(obj->string, (const char *)str, obj->length);

printf("Length = %d\n",(int)length); // Prints correct length
printf("!C string %s converted to wchar string %ls\n",str,obj->string); //obj->string is of a wcslen size larger than Length above...

if(length != wcslen(obj->string))
printf("Length failure!\n");

if(length == -1)
{
//Conversion failed, set string to NULL terminated character
free(obj->string);
obj->string = (wchar_t *)malloc(sizeof(wchar_t));
obj->string = L'\0';
}
else
{
//Conversion worked! but wcslen (and printf("%ls)) show the string is actually larger than length
//do stuff
}
}

最佳答案

您需要传递给 mbstowcs() 的长度包括 L'\0'终止符,但您在 obj->length() 中计算出的长度不包括它 - 您需要在传递给 mbstowcs() 的值上加 1 .

另外,不要使用strlen(str)要确定转换后的字符串的长度,您应该使用 mbstowcs(0, src, 0) + 1 .您还应该更改 str 的类型至 const char * ,并省略类型转换。 realloc()可以用来代替 free() / malloc()一对。总的来说,它应该是这样的:

typedef struct {
wchar_t *string;
size_t length;
} String;

void setCString(String *obj, const char *str)
{
obj->length = mbstowcs(0, src, 0);
obj->string = realloc(obj->string, (obj->length + 1) * sizeof(wchar_t));

size_t length = mbstowcs(obj->string, str, obj->length + 1);

printf("Length = %zu\n", length);
printf("!C string %s converted to wchar string %ls\n", str, obj->string);

if (length != wcslen(obj->string))
printf("Length failure!\n");

if (length == (size_t)-1)
{
//Conversion failed, set string to NULL terminated character
obj->string = realloc(obj->string, sizeof(wchar_t));
obj->string = L'\0';
}
else
{
//Conversion worked!
//do stuff
}
}

Mark Benningfield 指出 mbstowcs(0, src, 0)是 C 标准的 POSIX/XSI 扩展 - 要仅在标准 C 下获得所需的长度,您必须改为使用:
    const char *src_copy = src;
obj->length = mbstowcs(NULL, &src_copy, 0, NULL);

关于c - 将 char 转换为 wchar_t 的问题(长度错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3869035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com