QT中Unicode编码与字符串之间的转换
2011-06-20 11:35
274 查看
最近真的太忙了,所刚刚抽出时间,写点东西。前些日子再研究gprs,发短信时需要unicode编码转换,于是自己写了两个函数!可能有些麻烦!但挺好用的,也挺好理解的!下面就是我写的代码,希望可以帮助大家!
QString Widget::stringToUnicode(QString str)
{
// 这里传来的字符串一定要加tr,main函数里可以加 QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
// 例如:str=tr("你好");
const QChar *q;
QChar qtmp;
QString str0, strout;
int num;
q=str.unicode();
int len=str.count();
for(int i=0;i<len;i++)
{ qtmp =(QChar)*q++;
num= qtmp.unicode();
if(num<255)
strout+="00"; //英文或数字前加"00"
str0=str0.setNum(num,16);//变成十六进制数
strout+=str0;
}
return strout;
}
QString Widget::unicodeToString(QString str)
{
//例如 str="4F60597D";
int temp[400];
QChar qchar[100];
QString strOut;
bool ok;
int count=str.count();
int len=count/4;
for(int i=0;i<count;i+=4)
{
temp[i]=str.mid(i,4).toInt(&ok,16);//每四位转化为16进制整型
qchar[i/4]=temp[i];
QString str0(qchar, len);
strOut=str0;
}
return strOut;
}
QString Widget::stringToUnicode(QString str)
{
// 这里传来的字符串一定要加tr,main函数里可以加 QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
// 例如:str=tr("你好");
const QChar *q;
QChar qtmp;
QString str0, strout;
int num;
q=str.unicode();
int len=str.count();
for(int i=0;i<len;i++)
{ qtmp =(QChar)*q++;
num= qtmp.unicode();
if(num<255)
strout+="00"; //英文或数字前加"00"
str0=str0.setNum(num,16);//变成十六进制数
strout+=str0;
}
return strout;
}
QString Widget::unicodeToString(QString str)
{
//例如 str="4F60597D";
int temp[400];
QChar qchar[100];
QString strOut;
bool ok;
int count=str.count();
int len=count/4;
for(int i=0;i<count;i+=4)
{
temp[i]=str.mid(i,4).toInt(&ok,16);//每四位转化为16进制整型
qchar[i/4]=temp[i];
QString str0(qchar, len);
strOut=str0;
}
return strOut;
}
相关文章推荐
- QT中Unicode编码与字符串之间的转换
- Python中unicode编码的字符串和其他格式的字符串之间进行转换
- 字符串编码之间的转换(GB2312<->UTF8<->Unicode)及URLEncoding
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- 字符串编码之间的转换(GB2312<->UTF8<->Unicode)及URLEncoding .
- JSON: Python Objects与String之间转换【json.dumps把python任意数据类型转换成字符串,json.loads把字符串转换为unicode编码】
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- asii和unicode格式字符串之间的相互转换
- Ansi、Unicode、UTF8字符串之间的转换,wprintf
- Unicode编码与汉字字符串的转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- 字符串编码转换Unicode->GB2312
- 中日文和UNICODE之间编码的转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件