Howq to get the ascii of a char ?
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 :
Code:
int intValue = -123;
i whish to have the following string :
I is it possible to get the ascii code of a char ?
Thanks in advance.
Re: Howq to get the ascii of a char ?
Code:
#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.
Re: Howq to get the ascii of a char ?
Maybe something like this:
Code:
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:
Code:
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:
Code:
int val = 123;
for(int i=0;i<text.size();i++){
hex <<
QString::number(text
[i
].
toAscii(),
16);
}