简体   繁体   中英

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. I call my GetChar method on each char in the memory block to write to a 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). 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.

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? [Edit] I discovered that my mappings are actually extended ascii,, see the webpage 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)?

You could use a 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. see How to convert char* to 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. 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). 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. Here is an archived version of eXtreme if anyone needs it: http://members.i.net.net.au/~bertdb/ryan/eXtreme/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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