Convert wchar_t to char

后端 未结 9 1586
谎友^
谎友^ 2020-12-13 04:19

I was wondering is it safe to do so?

wchar_t wide = /* something */;
assert(wide >= 0 && wide < 256 &&);
char myChar = static_cast

        
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 04:27

    one could also convert wchar_t --> wstring --> string --> char

    wchar_t wide;
    wstring wstrValue;
    wstrValue[0] = wide
    
    string strValue;
    strValue.assign(wstrValue.begin(), wstrValue.end());  // convert wstring to string
    
    char char_value = strValue[0];
    

提交回复
热议问题