读书人

QT下16进制转换为字符串解决方法

发布时间: 2012-03-06 20:47:55 作者: rapoo

QT下16进制转换为字符串
各位大虾,今天又遇到一个难题,如下:
我想把一个unsigned int y=0xFF型的变量转换成字符串 "FF ",我用如下代码--------
#include <iostream.h>
#include <stdio.h>
#include <qstring.h>
#include <qvariant.h>
void verter(){
unsigned char y=0xFF;
QVariant z;
QString w;
z=y;
w=z.toString();
printf( "====================> the y is: %x\n ",y);
cout < < "--------------------> the string is: " < <w < <endl;
}
输出为:
====================> the y is: ff
--------------------> the string is:255
但是,我想要的是:FF的字符串,而不是255的字符串,请问各位大虾,有什么好建议么?



[解决办法]
给你一个小例子:

#include <iostream>

using namespace std;

int main(){
int y = 0xFF;

cout.unsetf( ios::dec );
cout.setf( ios::hex );
cout < < "the string is: " < < y < < endl;
}

编译:g++ -o test test.cpp
[解决办法]
sprintf(str, "%.02x ",value);

读书人网 >UNIXLINUX

热点推荐