Print value of char

S

Sumedha Swamy

Hi,
I would like to print the int value of Ü . The value is 220 (0xDC). Here is
a program that I worte.
But this prints the value 0. Where am I going wrong ?

#include <string>
#include <iostream>
using namespace std;
int main()
{
string a("\xDC");
cout << endl <<strtol(a.c_str(),NULL,10);
return 0;
}

Regards,
Sumedha
 
J

John Harrison

Sumedha said:
Hi,
I would like to print the int value of Ü . The value is 220 (0xDC). Here is
a program that I worte.
But this prints the value 0. Where am I going wrong ?

You are misunderstanding the purpose of strtol. strtol converts (for
instance) the string "12345" into the integer 12345
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a("\xDC");
cout << endl <<strtol(a.c_str(),NULL,10);
return 0;
}

Regards,
Sumedha

There is a much easier way to do what you want, just assign the
character to an integer (cast to unsigned char first to avoid the
possibility of getting a negative number).

int main()
{
int a = (unsigned char)'\xDC';
cout << a << endl;
}

john
 
S

Sumedha Swamy

Thanks John,
But I am dealing with a case where the "DC" that I have mentioned is read as
a string from a file. I would now like to interpret that value as an
integer. Thats why I created the small code shown above.
So, where am I going wrong?

--Sumedha
 
G

Greg

Sumedha said:
Thanks John,
But I am dealing with a case where the "DC" that I have mentioned is read as
a string from a file. I would now like to interpret that value as an
integer. Thats why I created the small code shown above.
So, where am I going wrong?

--Sumedha

DC is a base 16 numeric representation while the strtol routine
specifies base 10.

Greg
 
J

John Harrison

Sumedha said:
Thanks John,
But I am dealing with a case where the "DC" that I have mentioned is read as
a string from a file. I would now like to interpret that value as an
integer. Thats why I created the small code shown above.
So, where am I going wrong?

--Sumedha

You must disntguish these two cases. You can have a string "DC", in that
case you need to convert that string to an integer by interpreting the
string as a base 16 number.

string a("DC");
int value = strtol(a.c_str(), 0, 16);
cout << value << endl;

or you can have s tring containing the character '\xDC' (or 'Ü'), in
that case you just need to assign the character from the string to an
intger.

string a("\xDC");
int value = (unsigned char)a[0];
cout << value << endl;

Both of these print 220.

It seems a simple enough distinction, do you have a string with the
letter D followed by C (first case) or do you have string with the
letter Ü (second case)?

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top