1

我正在尝试使用 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* 字符串。

4

1 回答 1

2

我以前没有使用过 tpl,但是从快速查看文档来看,它似乎并不直接支持宽字符。您看到的字符串“1234”的行为与包含字节“1\x002\x003\x004\x00\x00\x00”的 UTF-16 编码字符串一致,tpl 将其视为 NUL 终止字节字符串“1\x00”。

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

  • 将 tpl 中的宽字符串表示为 16 位整数数组;
  • 将您的字符串编码为 UTF-8char字符串并使用 tpl 字符串类型;或者
  • 修改 tpl 以包含宽字符串类型。
于 2010-09-12T09:54:57.763 回答