Results 1 to 20 of 23

Thread: uchar* to int

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default uchar* to int

    Hello,

    How can I convert unsigned char* to int in Qt?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: uchar* to int

    one simple way
    char* str = "2012";
    int num = QString::fromLocal8Bit(str).toInt();
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: uchar* to int

    Thanks for reply, but Qt says "argument of type "uchar*" is incompatibile with parametr of "const uchar*""
    What can I do with my uchar*?

  4. #4
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: uchar* to int

    Yo

    unsigned char *ptr = (unsigned char*)"S";
    int num = (int)*ptr;

    print ptr & num

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: uchar* to int

    See QString::toInt.

    Quote Originally Posted by wyjyan View Post
    Yo

    unsigned char *ptr = (unsigned char*)"S";
    int num = (int)*ptr;

    print ptr & num
    I'm pretty sure that better to use C++ casting than C ones.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Aug 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: uchar* to int

    I totally agree so I used:

    uchar *poczatek;
    poczatek = obraz.bits();
    const char *adres = reinterpret_cast<const char*>(poczatek);
    int j = QString::fromLocal8Bit(adres).toInt();

    but I don't know why j = 0
    both pointers as well...

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: uchar* to int

    Lets clarify: what are you going to achieve? Do you need to convert a number which contains in a string (e.g. "12" => 12) or what?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Aug 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: uchar* to int

    Sorry

    I'm trying to

    get adres of first pixel of obraz image so I could use j
    as a starting point in loop:
    Qt Code:
    1. for(int i = j; i <= obraz.height(); i++){
    2. int licznik = 0;
    3. for(int k = 1; k <= obraz.width(); k++){
    4. QRgb tempColorRgb = obraz.pixel(k,i);
    5. QColor tempColor = QColor(tempColorRgb);
    6. wektor[i * obraz.width() + k + licznik] = tempColor.red();
    7. licznik++;
    8. wektor[i * obraz.width() + k + licznik] = tempColor.green();
    9. licznik++;
    10. wektor[i * obraz.width() + k + licznik] = tempColor.blue();
    11. if (k == obraz.width())
    12. k = 1;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: uchar* to int

    And you want to do...?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #10
    Join Date
    Aug 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: uchar* to int

    Hello,


    I've been very busy recently, I would like to back to my post:
    I would like to send as a parrameter to my assembler function adress of first red data (each pixel of obraz image is split to RGB)
    In order to achive this I have to convert uchar to int

    Qt Code:
    1. void PROJ_ASM_SEPIA::odcien_szarosci_sl()
    2. {
    3. // int *wektor = new int[obraz.height()*obraz.width()*3];
    4. uchar *poczatek;
    5. poczatek = obraz.bits(); //getting adress of first pixel in image obraz (uchar)
    6. const char *adres = reinterpret_cast<const char*>(poczatek); //convertion to const char
    7. int j = QString::fromLocal8Bit(adres).toInt(); //convertion char to int
    8.  
    9. for(int i = j; i <= obraz.height(); i++){ //adres of first pixel (j) as a starting point of the loop
    10. int licznik = 0;
    11. for(int k = 1; k <= obraz.width(); k++){ //every pixel is split to RGB data - red green blue - data is stored in wektor
    12. QRgb tempColorRgb = obraz.pixel(k,i);
    13. QColor tempColor = QColor(tempColorRgb);
    14. wektor[i * obraz.width() + k + licznik] = tempColor.red();
    15. licznik++;
    16. wektor[i * obraz.width() + k + licznik] = tempColor.green();
    17. licznik++;
    18. wektor[i * obraz.width() + k + licznik] = tempColor.blue();
    19. if (k == obraz.width())
    20. k = 1;
    21. }
    22. }
    23.  
    24.  
    25. odcien_szar(wektor[0],obraz.width(),obraz.height()); //assembler function calling with adress of first data (red of first pixel)
    26. }
    To copy to clipboard, switch view to plain text mode 


    The point is that the assembler function (gray scale) get adress of first pixel to make some calculations over rest of pixels.
    Last edited by Wojtek_SZ; 1st October 2012 at 12:13.

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: uchar* to int

    Show the definition of the function odcien_szar

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

    Default Re: uchar* to int

    Qt Code:
    1. odcien_szar((int)wektor[0],obraz.width(),obraz.height());
    To copy to clipboard, switch view to plain text mode 

    provided that wektor[0] is the address you want to pass to odcien_szar. Note that your odcien_szar function should take a uchar* and not an int, though. sizeof(int) doesn't have to be equal to sizeof(uchar*).
    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. Replies: 2
    Last Post: 19th July 2018, 06:38
  2. QImage from uchar buffer
    By StarShaper in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2012, 09:05
  3. save variable size hex string into uchar array
    By amika in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2011, 22:35
  4. Creating a QImage from uchar* data
    By forrestfsu in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2007, 15:21

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
  •  
Qt is a trademark of The Qt Company.