View Full Version : Howq to get the ascii of a char ?
yellowmat
27th April 2006, 10:05
Hi everybody,
I have an integer value and I need to convert each integer digit value to an ascii code. For example if I have :
int intValue = -123;
i whish to have the following string :
QString asciiValue = "2D 31 32 33"
I is it possible to get the ascii code of a char ?
Thanks in advance.
munna
27th April 2006, 10:50
#include <iostream>
using namespace std;
int main()
{
char x;
cout << "Enter character:\n";
cin >> x;
cout << "ASCII code for the letter is " << int(x) << ".\n";
return 0;
}
use ascii() to get the array of characters.
wysota
27th April 2006, 21:05
Maybe something like this:
int val = 123;
int rest;
int digit;
rest=val;
QVector<char> v;
while(rest>0){
digit = rest % 10; // last digit
rest = rest / 10;
v.push_front(digit+'0');
}
Then if you want their hex value, just sprintf them with "%x" formatting, like:
for(QVector<char>::const_iterator it = v.begin(); it!=v.end(); ++it){
::snprintf("%x", *it);
qDebug(qPrintable(hex));
}
Of course you can use QString::arg() for that.
You can also use QString methods directly like so:
int val = 123;
QString text = QString::number(val);
QStringList hex;
for(int i=0;i<text.size();i++){
hex << QString::number(text[i].toAscii(), 16);
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.