我有一个专有文件(数据库)格式,目前我正尝试迁移到SQL数据库。因此,我将文件转换为SQL转储,这已经正常工作。现在剩下的唯一问题是它们处理不在ASCII-decimal范围32到126内的字符的奇怪方式。它们具有以Unicode存储的所有这些字符的集合(十六进制 - 例如20AC =€),由它们自己索引内部索引。将Unicode的UTF8表示写入文件
我现在的计划是:我想创建一个表,其中存储内部索引,unicode(十六进制)和字符表示形式(UTF-8)。此表格可以用于未来的更新。
现在到问题:我如何写一个unicode十六进制值的UTF-8字符表示形式的文件?当前的代码如下所示:
this->outFile.open(fileName + ".sql", std::ofstream::app);
std::string protyp;
this->inFile.ignore(2); // Ignore the ID = 01.
std::getline(this->inFile, protyp); // Get the PROTYP Identifier (e.g. \321)
protyp = "\\" + protyp;
std::string unicodeHex;
this->inFile.ignore(2); // Ignore the ID = 01.
std::getline(this->inFile, unicodeHex); // Get the Unicode HEX Identifier (e.g. 002C)
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
const std::wstring wide_string = this->s2ws("\\u" + unicodeHex);
const std::string utf8_rep = converter.to_bytes(wide_string);
std::string valueString = "('" + protyp + "', '" + unicodeHex + "', '" + utf8_rep + "')";
this->outFile << valueString << std::endl;
this->outFile.close();
但这只是打印出这样的事:
('\321', '002C', '\u002C'),
虽然所需的输出将是:
('\321', '002C', ','),
我到底做错了什么?我不得不承认,当我提到字符编码和其他东西时,我并不确定:/。如果它有任何区别,我正在使用Windows 7 64位。 在此先感谢。
从'转换采取\ u002C'到宽字符值出现在编译的时候,不运行。你需要忘记'\ u'并做一个字符串到整数的转换。 –
它的工作原理!非常感谢。如果你愿意,你可以添加你的评论作为答案。我会尽快接受它。我还会添加一些代码作为第二个答案来介绍我提出的解决方案。 – puelo
我的目标是给你足够的信息来自己产生答案,看起来我成功了。您真诚的感谢是我需要的全部奖励。 –