Results 1 to 4 of 4

Thread: int to char conversion

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question int to char conversion

    Hi everyone,

    I am writing a program in Qt to send strings to mini panel printer through serial port.
    the panel printer only accept hex code.
    so, in my program, i have a function to send command to printer as follow:
    Qt Code:
    1. ls.append(0x1B + 0x27 + 0x00 + 0x0D);
    2. strSend(ls);
    To copy to clipboard, switch view to plain text mode 
    I have some parameters (int values) need to send to printer as well.
    for example, i have:
    int a = 3;
    int b = 53;
    I want to convert them to 0x03 and 0x35, and store in a_char and b_char. then i can do:
    Qt Code:
    1. ls.append(0x1B + 0x27 + 0x00 + 0x0D + a_char + b_char);
    2. strSend(ls);
    To copy to clipboard, switch view to plain text mode 

    I saw someone in this forum mentioned about itoa (a,b,16), but Qt gave me error message
    error: ‘itoa’ was not declared in this scope
    (I did include the stdlib.h)

    Can anyone else give me a hint please?
    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: int to char conversion

    the panel printer only accept hex code.
    The panel accepts a series of bytes. For your convenience you can express them in decimal, hexadecimal, octal, or ASCII characters in your source code. The panel doesn't care, it just accepts the bytes.

    You don't tell us what type 'ls' is, but QByteArray is probably the best match for your task.
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QCoreApplication app(argc, argv);
    6.  
    7. int a = 3;
    8. int b = 53;
    9.  
    10. ls += 0x1b; // hex
    11. ls += 39; // decimal
    12. ls += '\0'; // as a character
    13. ls += 015; // octal
    14. ls += static_cast<char>(a); // explicit cast from int
    15. ls += b; // implicit cast from int
    16. // send the byte array
    17.  
    18. qDebug() << ls.toHex();
    19.  
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 
    You could more conveniently initialise the fixed data in the array like:
    Qt Code:
    1. QByteArray ls = QByteArray::fromHex("1b27000d");
    To copy to clipboard, switch view to plain text mode 
    then append a and b.
    Last edited by ChrisW67; 14th June 2011 at 01:42.

  3. #3
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: int to char conversion

    Hi ChrisW67,

    Thanks for your quick reply. I will try it.
    One more question. if i want to send a string to printer, do i just do

    Qt Code:
    1. ...
    2. ls += 'my string';
    3. ...
    To copy to clipboard, switch view to plain text mode 

    I really appreciate your help.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: int to char conversion

    That won't compile, or won't work if it does. Try double quotes: single quotes are for character literals.

    Does your printer take ASCII only, or does it do Unicode through UTF-8? If it does do Unicode then using QString to build your output and then toUtf8() might also work.

Similar Threads

  1. Problem in conversion of QChar to char
    By Prashant_Bhutani in forum Newbie
    Replies: 5
    Last Post: 19th April 2010, 10:27
  2. Char* <-> QString implicit conversion
    By kingfinn in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2010, 08:26
  3. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13: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
  •  
Qt is a trademark of The Qt Company.