gpt4 book ai didi

c - 使用 TPL 在结构中序列化 wchar_t*

转载 作者:太空宇宙 更新时间:2023-11-04 03:07:03 24 4
gpt4 key购买 nike

我正在尝试使用 tpl 序列化包含 wchar_t* 字符串的结构。

我的代码看起来像这样,但它不起作用:

#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <wchar.h>
#include "tpl.h"

struct chinese_t {
wchar_t *chars;
};


int main() {

tpl_node *tn;


struct chinese_t cstr;
cstr.chars = L"字符串";

tn = tpl_map("S(s)", &cstr);
tpl_pack( tn, 0 );
tpl_dump(tn, TPL_FILE, "string.tpl");
tpl_free(tn);


struct chinese_t cstr2;

tn = tpl_map( "S(s)", &cstr2);
//tpl_load(tn, TPL_MEM, buffer, len);
tpl_load(tn, TPL_FILE, "string.tpl");
tpl_unpack(tn, 0);
tpl_free(tn);


printf("%ls\n", cstr2.chars);
return;
}

如果我用“1234”替换中文“字符串”字符串,它只会打印“1”——如果我更改定义以便结构使用 char*(并且我只将 ASCII 字符放入其中)它就可以工作正好。但是我不知道如何让它正确地序列化和反序列化 wchar_t* 字符串。

最佳答案

之前没用过tpl,不过快速浏览了一下文档,好像没有直接支持宽字符。您看到的字符串“1234”的行为与包含字节“1\x002\x003\x004\x00\x00\x00”的 UTF-16 编码字符串一致,被 tpl 视为仅以 NUL 终止的字节字符串“1\x00”。

看起来您最好的选择可能是:

  • 将 tpl 中的宽字符串表示为 16 位整数数组;
  • 将您的字符串编码为 UTF-8 char 字符串并使用 tpl 字符串类型;或
  • 修改 tpl 以包含宽字符串类型。

关于c - 使用 TPL 在结构中序列化 wchar_t*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694349/

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