天天看点

C++常用类型转换(在Unicode和MBCS之间相互转换函数)

1、C++在Unicode和MBCS之间相互转换函数

BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
{
        DWORD dwMinSize;
        dwMinSize = MultiByteToWideChar(CP_ACP, 0, lpcszStr, -1, NULL, 0);
        if (dwSize < dwMinSize)
        {
                return FALSE;
        }
        MultiByteToWideChar(CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
        return TRUE;
}
BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
{
        DWORD dwMinSize;
        dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
        if (dwSize < dwMinSize)
        {
                return FALSE;
        }
        WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwSize, NULL, FALSE);
        return TRUE;
}
使用方法:
wchar_t wText[255] = {L"你好"};
char sText[255]= {0};
WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0]));//宽字节转多字节
MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0]));//多字节转宽字节      

2、std:string的格式化

#ifndef NOT_SAVE_IMAGE
    std::string name;
    std::stringstream ss1;
    ss1.str("");
    ss1 << "imageSrc";
    ss1 << index;
    ss1 << ".jpg";
    name = ss1.str();
    _saveImageMat(name, imageSrc);
    ss1.str("");
    ss1 << "imageDst";
    ss1 << index;
    ss1 << ".jpg";
    name = ss1.str();
    _saveImageMat(name, imageDst);
#endif
void CGPUTest::_saveImageMat(std::string path, const cv::Mat& img)
{
    //根据扩展名来决定储存jpg还是bmp
    if (path.find("jpg") != path.npos)//找到了jpg,一个特别的标志c++中用npos表示
    {
  vector<int> compression_params;
  compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);//选择jpeg
  compression_params.push_back(100);//在这个填入你要的图片质量,默认值是95,100代表满分
  cv::imwrite(path, img, compression_params);//根据路径,保存为jpg格式
    }
    else
    {
  cv::imwrite(path, img);//根据路径,保存为bmpg格式
    }
}      

继续阅读