i want to convert a string to an integer value... for example :: if there is a string called "Guy"

G=7; // in the alpherbert G is = 7
u=21;
y=24;

i want the out put to be as >>

7 21 24


can some one plzz help me to write this code >>> :(

Recommended Answers

All 5 Replies

typecast each character, such as

std::string x = "guy";
cout << (int)x[0];

Now, just put that in a loop, replace the 0 in [0] with loop counter and you have it.

typecast each character, such as

std::string x = "guy";
cout << (int)x[0];

Now, just put that in a loop, replace the 0 in [0] with loop counter and you have it.

Thanx Dragon

If you are using a c style null terminated string, you can cast them into an integer to display the ascii value, and just bias it however you feel.

ie

int iUpperBias(64); //ascii for G(71) - 64 = 7 like you desire
int iLowerBias(96)
char * szString;
//fill with some data
cin >> szString;
for (int i(0); i < strlen(szString) - 1; ++i)
{
     if (szString[i] > iLowerBias && szString[i] <= iLowerBias + 26)
           cout << static_cast<int>(szString[i]) - iLowerBias << " ";
     if (szString[i] > iUpperBias && szString[i] <= iUpperBias + 26)
           cout << static_cast<int>(szString[i]) - iUpperBias << " "; 
}

If you are using a c style null terminated string, you can cast them into an integer to display the ascii value, and just bias it however you feel.

ie

int iBias(-64); //ascii for G(71) - 64 = 7 like you desire
char * szString;
//fill with some data
cin >> szString;
for (int i(0); i < strlen(szString) - 1; ++i)
     cout << static_cast<int>(szString[i]) + iBias << " ";

Thnx alot.. !!! :)

I changed it to work with lower and upper case values ^^^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.