zoukankan      html  css  js  c++  java
  • string转utf8后解决TTS识别中文的问题

    今天遇到string字符编码的问题,由于遇到了用TTS将文本转语音的一个API,里面的中文必须是utf8的,我传了一个uncode编码的中文进去,就一直不能正常读出来。后来才发现是编码的问题。这里在网上找到两个API,可将string 传成utf8编码的string。挺好用的。

    记录下来:


    std::string string_To_UTF8(const std::string & str)
    {
        int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    
        wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
        ZeroMemory(pwBuf, nwLen * 2 + 2);
    
        ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
    
        int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
    
        char * pBuf = new char[nLen + 1];
        ZeroMemory(pBuf, nLen + 1);
    
        ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
    
        std::string retStr(pBuf);
    
        delete []pwBuf;
        delete []pBuf;
    
        pwBuf = NULL;
        pBuf  = NULL;
    
        return retStr;
    }
    BOOL IsTextUTF8(char* str,ULONGLONG length)
    {
        DWORD nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节
        UCHAR chr;
        BOOL bAllAscii=TRUE; //假设所有都是ASCII, 说明不是UTF-8
        for(int i=0; i<length; ++i)
        {
            chr= *(str+i);
            if( (chr&0x80) != 0 ) // 推断是否ASCII编码,假设不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx
                bAllAscii= FALSE;
            if(nBytes==0) //假设不是ASCII码,应该是多字节符,计算字节数
            {
                if(chr>=0x80)
                {
                    if(chr>=0xFC&&chr<=0xFD)
                        nBytes=6;
                    else if(chr>=0xF8)
                        nBytes=5;
                    else if(chr>=0xF0)
                        nBytes=4;
                    else if(chr>=0xE0)
                        nBytes=3;
                    else if(chr>=0xC0)
                        nBytes=2;
                    else
                        return FALSE;
    
                    nBytes--;
                }
            }
            else //多字节符的非首字节,应为 10xxxxxx
            {
                if( (chr&0xC0) != 0x80 )
                    return FALSE;
    
                nBytes--;
            }
        }
        if( nBytes > 0 ) //违返规则
            return FALSE;
        if( bAllAscii ) //假设所有都是ASCII, 说明不是UTF-8
            return FALSE;
    
        return TRUE;
    }

  • 相关阅读:
    Django models中的null和blank的区别
    3次登陆锁定与backend增删改查
    Python全栈考试(一)
    python第一天几个小游戏
    linux开发脚本自动部署及监控
    Linux awk&shell script
    linux正则表达式grep&sed
    linux网路IP.设定主机名.ssh .bash命令&通配符
    Linux:nginx(web服务),nfs服务+反向代理+负载均衡
    Linux内存dd,rpm,yum,软件安装
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7083265.html
Copyright © 2011-2022 走看看