Results 1 to 3 of 3

Thread: byte to uint

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default byte to uint

    Hi, I'm trying to rewrite some functions from C# to Qt, but something is going wrong. I dont know why...
    Could somebody look in my code please ?

    Function should convert byte to uint:

    Orginal C# function:
    Qt Code:
    1. public static String ByteToString(byte[] crc)
    2. {
    3. String ret = "";
    4. if(crc != null){
    5. ret = ((int)crc[0]).ToString("X2");
    6. ret += ((int)crc[1]).ToString("X2");
    7. ret += ((int)crc[2]).ToString("X2");
    8. ret += ((int)crc[3]).ToString("X2");
    9. }
    10. return ret;
    11. }
    12.  
    13. public static uint ByteTouInt(byte[] crc){
    14. if(crc != null) {
    15. return Convert.ToUInt32(Tools.ByteToString(crc), 16);
    16. }else{
    17. return 0;
    18. }
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 

    My function in Qt (First idea)
    Qt Code:
    1. QString Tools::byteToString(byte *crc)
    2. {
    3. QString crc_string = "";
    4. if(sizeof crc < 4 || crc == NULL) {
    5. return "bad";
    6. }
    7. for(int i = 0; i < 4; i++) {
    8. crc_string += (QString::number((int)crc[i], 16).length() == 1) ? "0" + QString::number((int)crc[i], 16) : QString::number((int)crc[i], 16);
    9. }
    10. return crc_string.toUpper();
    11. }
    12.  
    13. inline uint byteToUInt(byte *crc) {
    14. return byteToString(crc).toUInt(NULL, 16);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Second idea...
    Qt Code:
    1. inline uint Tools::byteToUInt(byte *b)
    2. {
    3. uint val;
    4. ba.append(((const char*) b));
    5.  
    6. //val = (ba[1] << 8) | ba[0];
    7. memcpy(&val, ba.constData(), 2);
    8.  
    9. return val;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Function returns different values, I'm not a author of C# code, so I've got problems to convert it properly.
    To compile C# code I used this online IDE/compiler: http://www.tutorialspoint.com/compile_csharp_online.php

    Thank You for any help and suggestions.

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: byte to uint

    Hi Dragon,
    Your code convert array of char (4byte) to hex and convert that hex to uint datatype.
    This is a simple code how to do that :
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <stdio.h>
    4.  
    5. QString ByteToString(uchar *crc)
    6. {
    7. QString ret = "";
    8. QString str;
    9.  
    10. ret = str.setNum((crc[0]), 16);
    11. ret += str.setNum((crc[1]), 16);
    12. ret += str.setNum((crc[2]), 16);
    13. ret += str.setNum((crc[3]), 16);
    14.  
    15. return ret;
    16. }
    17.  
    18. uint ByteTouInt(uchar *crc)
    19. {
    20. if (crc != NULL)
    21. {
    22. bool ok;
    23. return (ByteToString(crc).toUInt(&ok, 16));
    24. }
    25. else
    26. {
    27. return 0;
    28. }
    29. }
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QCoreApplication a(argc, argv);
    34. uchar aa[] = {40,50,60,70};
    35. ByteToString(aa);
    36. uint result = ByteTouInt(aa);
    37. qDebug()<<"Input : " << aa[0]<<aa[1]<<aa[2]<<aa[3];
    38. qDebug()<<"Result : "<< result;
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    This is the sample result :
    Input : 40 50 60 70
    Result : 674380870

    Please try to read my code to get the algorithm related with your problem.
    Hope this code can help you.
    Thank you.

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

    #Dragon (27th October 2015)

  4. #3
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: byte to uint

    Thanks for help
    Regards

Similar Threads

  1. Replies: 10
    Last Post: 29th August 2014, 10:02
  2. Replies: 3
    Last Post: 19th April 2010, 15:16
  3. converting uint to QString
    By Yayati.Ekbote in forum Newbie
    Replies: 1
    Last Post: 14th April 2010, 14:43
  4. (portion of...) QByteArray to UInt
    By pdoria in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2009, 16:23
  5. void clearWState( uint n );
    By sunil.thaha in forum Qt Programming
    Replies: 4
    Last Post: 4th February 2006, 17:12

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.