Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: uchar* to int

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

    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
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    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
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    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
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    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,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: uchar* to int

    Show the definition of the function odcien_szar

  12. #12
    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: 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.


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

    Default Re: uchar* to int

    Hello
    here is the code of odcien_szar and sepia as well:


    Qt Code:
    1. include filtry.inc
    2.  
    3. .code
    4.  
    5. ;*********************************************************************************************************************************************************************************************************************************************************************
    6. ; ODCIEŃ SZAROŚCI
    7.  
    8. ;*********************************************************************************************************************************************************************************************************************************************************************
    9.  
    10. pushad
    11.  
    12. odcien_szar PROC uses ebx edx, w_bmp :DWORD, szerokosc :DWORD, wysokosc :DWORD
    13.  
    14.  
    15.  
    16.  
    17. mov eax, szerokosc ;ładowanie do rejestru szerokości obrazu.
    18. mov ebx, wysokosc ;ładowanie do rejestru wysokości obrazu
    19. mul ebx
    20. xor ebx, ebx
    21. mov ebx, 3 ;RGB - RED, GREEN, BLUE
    22. mul ebx
    23.  
    24. mov esi, w_bmp ;wskaźnik do początku tablicy bajtów obrazu bitmapy.
    25. mov ecx, eax ;ecx - liczba bajtów do przetworzenia
    26.  
    27. add ecx, esi
    28.  
    29. assume esi:ptr byte
    30.  
    31. petla: xor eax, eax
    32. xor ebx, ebx
    33.  
    34. mov al, [esi] ;Å‚adowanie R
    35. mov bl, [esi+1] ;Å‚adowanie G
    36. add eax, ebx ;sumowanie R + G
    37. mov bl, [esi+2] ;Å‚adowanie B
    38. add eax, ebx ;sumowanie R + G + B
    39. xor ebx, ebx
    40. mov ebx, 3
    41. div ebx
    42. mov [esi], al ;zapisywanie R
    43. mov [esi+1], al ;zapisywanie G
    44. mov [esi+2], al ;zapisywanie B
    45. add esi, ebx ;zwiększanie adresu
    46. cmp ecx, esi ;porównywanie zawartości dwóch rejestrów
    47. ja petla ;skocz na początek jeśli ecx > esi
    48.  
    49. popad
    50. ret
    51. odcien_szar endp
    52.  
    53. end
    54.  
    55. ;*********************************************************************************************************************************************************************************************************************************************************************
    56. ; SEPIA
    57.  
    58. ;*********************************************************************************************************************************************************************************************************************************************************************
    59.  
    60.  
    61. pushad
    62.  
    63. sepia PROC uses eax ebx edx esi edi, w_bmp :DWORD, szerokosc :DWORD, wysokosc :DWORD
    64.  
    65.  
    66. invoke odcien_szar, w_bmp, szerokosc, wysokosc ;wywołanie procedury
    67.  
    68.  
    69. mov eax, szerokosc ;ładowanie do rejestru szerokości obrazu.
    70. mov ebx, wysokosc ;ładowanie do rejestru wysokości obrazu
    71. mul ebx
    72. xor ebx, ebx
    73. mov ebx, 3 ;RGB - RED, GREEN, BLUE
    74. mul ebx
    75.  
    76. mov esi, w_bmp ;wskaźnik do początku tablicy bajtów obrazu bitmapy.
    77. mov ecx, eax
    78. add ecx, esi
    79.  
    80. assume esi:ptr byte
    81.  
    82. petla: xor eax, eax
    83. xor ebx, ebx
    84. ;mov eax, parametr
    85. mov ebx, 2
    86. mul ebx
    87. mov bl, [esi]
    88. add eax, ebx
    89. mov [esi], al
    90.  
    91. xor eax, eax
    92. xor ebx, ebx
    93. ;mov eax, parametr
    94. mov bl, [esi+1]
    95. add eax, ebx
    96. mov [esi+1], al
    97.  
    98. xor ebx, ebx
    99. mov ebx, 3
    100. add esi, ebx
    101. cmp ecx, esi ;porównywanie zawartości dwóch rejestrów
    102. ja petla ;skocz na początek jeśli ecx > esi
    103.  
    104. popad
    105. ret
    106. sepia endp
    107. end
    To copy to clipboard, switch view to plain text mode 



    Wysota (Master of Zen) said:

    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*).
    My intention is to send to function odcien_szar and sepia vector of binary representing data (RGB) so the further processing may take palce.
    Last edited by Wojtek_SZ; 1st October 2012 at 17:17.

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

    Default Re: uchar* to int

    Show a C header definition of this function not body.

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

    Default Re: uchar* to int

    sorry c header of which function?
    Spia and odcien_szar are functions stored in file with .asm extention, thus they hasn't got a c ext.

    here is code of header PROJ_ASM_SEPIA class:
    Qt Code:
    1. #ifndef PROJ_ASM_SEPIA_H
    2. #define PROJ_ASM_SEPIA_H
    3. #pragma once
    4. #include <QtGui/QMainWindow>
    5. #include <QMenu>
    6. #include <qfiledialog.h>
    7. #include <qmessagebox.h>
    8. #include "ui_proj_asm_sepia.h"
    9. #include <QBitmap>
    10. #include <QLabel>
    11. #include <QScrollArea>
    12. #include<QString>
    13.  
    14.  
    15.  
    16.  
    17.  
    18. //#include "resource.h"
    19. #include <windows.h>
    20. extern "C" void _stdcall odcien_szar (DWORD x, DWORD y, DWORD z);
    21. extern "C" void _stdcall sepia (DWORD x, DWORD y, DWORD z);
    22.  
    23. class PROJ_ASM_SEPIA : public QMainWindow
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. PROJ_ASM_SEPIA(QWidget *parent = 0, Qt::WFlags flags = 0);
    29. ~PROJ_ASM_SEPIA();
    30. signals:
    31.  
    32. public slots:
    33. void wyjscie();
    34. void komunikat_o_programie();
    35. void instrukcje();
    36. void otworz_plik();
    37. void odcien_szarosci_sl();
    38. void sepia_sl();
    39. private:
    40. QImage obraz;
    41. Ui::PROJ_ASM_SEPIAClass ui;
    42. int konwersja(uchar *adres);
    43. };
    44.  
    45. #endif // PROJ_ASM_SEPIA_H
    To copy to clipboard, switch view to plain text mode 

    here is the file tree of my vs solution
    file tree.jpg
    Last edited by Wojtek_SZ; 2nd October 2012 at 10:23.

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

    Default Re: uchar* to int

    OK, first parameter (x) should be an address or value ?

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

    Default Re: uchar* to int

    yes, exactly.

    As the cell in memory are numbered by 0x values, I would like change this value to int (decimal) and sent the starting point of my vector to assembler function

  18. #18
    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: uchar* to int

    Quote Originally Posted by Wojtek_SZ View Post
    sent the starting point of my vector
    That's an address, not a value.
    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.


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

    Default Re: uchar* to int

    sorry I just lost myself....
    Let' try from the other hand. How to send to function odcien_szar (acording to parameters of this function) whole information about every color rgb i every pixel?

  20. #20
    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: uchar* to int

    I would send QImage::bits() which is an address of a memory area containing pixel data for an image. Thus passing it as int albeit will succeed, is not a good thing to do because it is not a value but rather an address (aka pointer).
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.