简体   繁体   English

如何在 c++ 中将扩展的 ascii 字符从 char 转换为 wstring

[英]How to convert extended ascii characters from char to wstring in c++

I read a file using ifstream's read method into a char* memory block.我使用 ifstream 的读取方法将文件读取到 char* memory 块中。 I call my GetChar method on each char in the memory block to write to a wstring.我对 memory 块中的每个字符调用我的 GetChar 方法以写入 wstring。 I am trying to write the unicode characters to the screen (at least I think they are unicode, please correct me if I am wrong - oh no it looks like I was wrong its extended ascii).我正在尝试将 unicode 个字符写入屏幕(至少我认为它们是 unicode,如果我错了请纠正我 - 哦不,看起来我错了它的扩展 ascii)。 Unfortunately the only way I've got it to work is to hard code the unicode characters in a switch statement, but I'd rather it work for any character, not just the ones I've encountered and added by hard coding them.不幸的是,我让它工作的唯一方法是在 switch 语句中对 unicode 字符进行硬编码,但我宁愿它适用于任何字符,而不仅仅是我遇到并通过硬编码添加的字符。

Here is what I'm currently using:这是我目前正在使用的:

std::wstring GetChar(char o)
{
  switch (o)
  {
  case 0x0D:
    return L"♪";
  case 0x0A:
    return L"◙";
  case -38:
    return L"┌";
  case -60:
    return L"─";
  case -77:
    return L"│";
  case -65:
    return L"┐";
  case -61:
    return L"├";
  case -76:
    return L"┤";
  case -2:
    return L"■";
  }

  std::wstring tmp_string(1, o);
  return tmp_string;
}

Any idea how to convert -38 to L"┌" in a generic way?知道如何以通用方式将 -38 转换为 L"┌" 吗? [Edit] I discovered that my mappings are actually extended ascii,, see the webpage https://www.sciencebuddies.org/science-fair-projects/references/ascii-table [编辑]我发现我的映射实际上是扩展的ascii,,见网页https://www.sciencebuddies.org/science-fair-projects/references/ascii-table

I think what I will try is to create a txt file with extended ascii mapping based on this webpage: https://theasciicode.com.ar/ Is there is a simpler programmatic way (eg with setlocale)?我想我会尝试创建一个基于此网页的扩展 ascii 映射的 txt 文件: https://theasciicode.com.ar/是否有更简单的编程方式(例如使用 setlocale)?

You could use a std::map<char,wstring>.您可以使用 std::map<char,wstring>。

wstring ConvertChar (const char target)
{
    static const std::map<char, wstring> convert = {{38,L"┌"}, {76, L"┤"} ....
    auto target = convert.find(target);
    if (target != convert.end())
        return *target;
    return L" ";// NOT found
}

Or there are functions that do the conversion between char and wide.或者有一些函数可以在 char 和 wide 之间进行转换。 see How to convert char* to wchar_t*?请参阅如何将 char* 转换为 wchar_t*?

I got a solution I am happy with.我得到了一个令我满意的解决方案。 I wrote a key file containing bytes 00 to FF, then I viewed the file with eXtreme (no longer exists) which could show the extended ascii values which I then copied into Notepad++ and saved as unicode values file.我写了一个包含字节 00 到 FF 的密钥文件,然后我用 eXtreme(不再存在)查看了该文件,它可以显示扩展的 ascii 值,然后我将其复制到 Notepad++ 中并保存为 unicode 值文件。 I then had mapping for 0x00 to 0xFF to their nice looking extended ascii characters (saved as unicode) all displaying well as wstrings (no hard coded values).然后我将 0x00 到 0xFF 映射到它们漂亮的扩展 ascii 字符(保存为 unicode),所有这些都显示得很好,就像 wstrings(没有硬编码值)。 I may want to support regular unicode too some day as another mode, but extended ascii is good enough for the files I'm currently working with.有一天我可能也想支持常规的 unicode 作为另一种模式,但扩展的 ascii 足以满足我当前正在使用的文件。 Here is an archived version of eXtreme if anyone needs it: http://members.i.net.net.au/~bertdb/ryan/eXtreme/如果有人需要,这里是 eXtreme 的存档版本: http://members.i.net.net.au/~bertdb/ryan/eXtreme/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM