Results 1 to 3 of 3

Thread: Converting from char to byte

  1. #1
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Converting from char to byte

    Hi,

    I would like to create a random packet of data, 128 bits long (16bytes).

    My basic idea is to generate 16 pseudo-random numbers (between 0 and 255), then turn each of these numbers into their equivalent bytes. Then I intend to put these 16 bytes end to end to create my packet.

    This is what I have so far:

    Qt Code:
    1. void Loader::calculateRandomCode()
    2. {
    3. int n;
    4. n = qrand() % 255; // limits the random number to between 0 and 255
    5. QString qs = QString::number(n); // converts from int to string
    6. QLabel *label = new QLabel(qs); // prints to screen just so I can see whats going on
    7. label->show(); // prints to screen just so I can see whats going on
    8. }
    To copy to clipboard, switch view to plain text mode 


    How do I turn n into a byte?


    Thanks for reading

  2. #2
    Join Date
    Jan 2010
    Location
    New Zealand
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting from char to byte

    well a int is 4 bytes long in 32bit architecture and 8 bytes in 64bit architecture so what you should do it instead of using int is try using "unsigned char". and cast the result of qrand to "unsigned char"
    Qt Code:
    1. unsigned char n;
    2. n = (unsigned char) qrand() % 255;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to bythos for this useful post:

    Ferric (8th January 2010)

  4. #3
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting from char to byte

    Did you look at quint8, quint16, quint32 or quint64? You can use quint32[4] to have you 128 bits or quint8[16] to process that byte by byte.
    -- Tanuki

    per cauda vel vaculus cauda

Similar Threads

  1. Converting QString to char* in onl line
    By hubbobubbo in forum Qt Programming
    Replies: 10
    Last Post: 11th December 2009, 11:45
  2. Converting QString to char array
    By srohit24 in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 18:19
  3. problem converting string-char-int
    By mickey in forum General Programming
    Replies: 1
    Last Post: 9th December 2006, 23:43
  4. QString to char* not converting
    By DPinLV in forum Qt Programming
    Replies: 17
    Last Post: 6th August 2006, 12:15
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.