Results 1 to 4 of 4

Thread: Convert int to Hex with fixed bytes

  1. #1
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Convert int to Hex with fixed bytes

    Hi,
    i need to convert an int to hex and after to string but i need to do it with a fixed number of bytes per example:

    int a=12;
    qDebug()<<QString::number(tam, 16);
    =="C" (it is ok)

    but i need that the string always have for chars like= "000C"

    There is an easy solution for it?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert int to Hex with fixed bytes

    Yes, read the whole documentation for the function you are using, you can pass more than two parameters to it
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Convert int to Hex with fixed bytes

    Quote Originally Posted by wysota View Post
    Yes, read the whole documentation for the function you are using, you can pass more than two parameters to it
    i tried this: qDebug()<<QString::number(tam, 16, 4); and result is:
    "12.0000" :s

    EDIT1
    I'm seeing this: QString QString::arg ( ulong a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

    EDIT2
    qDebug()<<QString("%1").arg(tam,4,16,QLatin1Char(' 0'));

    i don't know if this is best way but works

    "000c"
    Last edited by metRo_; 7th September 2011 at 16:27.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert int to Hex with fixed bytes

    It's ok. You can also do:
    Qt Code:
    1. QString c = QString::number(tam, 16);
    2. if(c.size()<4) c = QString(4-c.size(), '0')+c;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QString in bytes
    By estanisgeyer in forum Newbie
    Replies: 1
    Last Post: 23rd October 2010, 16:04
  2. How to get an array of bytes from a DLL?
    By srohit24 in forum Qt Programming
    Replies: 5
    Last Post: 22nd April 2009, 16:11
  3. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 07:38
  4. How many bytes did i send?
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2008, 10:12
  5. is ther any function which use int 8 bytes
    By jyoti in forum General Programming
    Replies: 3
    Last Post: 12th December 2006, 10:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.