日期:2014-05-16  浏览次数:21695 次

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);