首页 > 编程语言 >C++的String与UF8互转

C++的String与UF8互转

时间:2023-09-10 23:38:25浏览次数:50  
标签:String UTF8 pBuf UF8 pwBuf str nwLen 互转 NULL

  • UTF8_To_String
#include<Stringapiset.h>
#include <iostream>

std::string UTF8_To_String(const std::string& str)
{
    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);

    wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 
    memset(pwBuf, 0, nwLen * 2 + 2);

    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

    int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

    char* pBuf = new char[nLen + 1];
    memset(pBuf, 0, nLen + 1);

    WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string retStr = pBuf;

    delete[]pBuf;
    delete[]pwBuf;

    pBuf = NULL;
    pwBuf = NULL;

    return retStr;
}
  • String_To_UTF8
#include<Stringapiset.h>
#include <iostream>
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;
}

技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!

开源库地址,欢迎点亮:

GitHub: https://github.com/ITMingliang

Gitee:  https://gitee.com/mingliang_it

GitLab:  https://gitlab.com/ITMingliang

建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群

C++的String与UF8互转_C++


标签:String,UTF8,pBuf,UF8,pwBuf,str,nwLen,互转,NULL
From: https://blog.51cto.com/u_16244728/7428553

相关文章

  • JSON.stringify和JSON.parse的用法和区别
    JSON.stringify()和JSON.parse()是JavaScript中用于处理JSON数据的方法,它们的用法和区别如下:####一:JSON.stringify()方法将JavaScript对象或值转换为JSON字符串。它接受一个参数,即要转换的对象或值。示例:varobj={name:'John',age:25};varjsonString=JSON.......
  • Java常用类-String
    String保存的是字符串常量,值不能被修改,每次更新都会重新开辟空间,创建对象、重新指向,效率较低。所以提供了StringBuilder和StringBuffer来增强String的功能。privatefinalcharvalue[]常用方法 equalsIgnoreCase()//忽略大小写判断是否相等indexOf()//字符在字符串中第......
  • [AGC058D] Yet Another ABC String
    [AGC058D]YetAnotherABCStringAtcoder:[AGC058D]YetAnotherABCString洛谷:[AGC058D]YetAnotherABCStringProblem给出\(a,b,c\),求由\(a\)个A,\(b\)个B,\(c\)个C构成的字符串数量,使得不存在子串ABC,BCA和CAB。\(1\leqa,b,c\leq10^6\)。Solution可能是......
  • Go语言反单引号创建原始字符串raw_string
    在Go语言中,反引号(`)用于创建原始字符串字面量(rawstringliterals)。原始字符串字面量是不包含转义序列的字符串,其中的内容会保持原样,包括换行符和特殊字符。反引号的主要用途之一是编写多行的文本或正则表达式,而不需要使用转义字符。这在处理包含大量特殊字符或格式化要求的文本......
  • Set<String>怎么样赋值
    如果您是在Java中使用Set<String>类型,可以使用以下方法来赋值:使用构造函数赋值:Set<String>set=newHashSet<>(Arrays.asList("value1","value2","value3"));使用add()方法逐个添加元素:Set<String>set=newHashSet<>();set.add("......
  • 手撕代码,实现String类的构造函数、拷贝构造函数、赋值构造函数以及析构函数
    #include<bits/stdc++.h>usingnamespacestd;classString{public:String(constchar*str=NULL){//普通构造函数cout<<"普通构造函数被调用"<<endl;if(str==NULL){data=newchar[1];*dat......
  • java的List调用toString以后去除两端[]括号
    原始代码:List<Integer>vids=newArrayList<>();if(planList!=null&&planList.size()>0){for(inti=0;i<planList.size();i++){VoicePlanListEntityvoicePlanListEntity=newVoi......
  • (Java)String截取指定字符前面(后面)所有字符和String的常用方法
    1获取String的相关信息功能1)length():获取该字符串长度Stringstr="group-banner-top-";intlength=str.length();System.out.println(length);运行结果:172)charAt(intindex):获取指定索引处字符Stringstr="group-banner-top-";System.out.println(str.charAt(7)......
  • java中String和数组的长度
    数组的长度是lengthString的长度是length()在Java中,数组是引用数据类型,不是类,因此也是读取固有的length属性得到数组长度,它没有length()方法。但是,java中的String类型是jdk中已经封装好的final类,类就有属性和方法,只是String没有length属性,只有length()方法。......
  • D. Balanced String
    D.BalancedStringYouaregivenabinarystring$s$(abinarystringisastringconsistingofcharacters0and/or1).Let'scallabinarystringbalancedifthenumberofsubsequences01(thenumberofindices$i$and$j$suchthat$1\lei<j......