Get the ascii value of a string


I am wanting to get the value of my string to send as a checksum.
(I found atoi will not work.  I have also tried __isascii but maybe I am not using it correctly.)

Can you please give me some help with this?

Thank you
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

cgallagherConnect With a Mentor Commented:
To get the ascii value of a "char" the following will work:

int i;
char c='A';
i = (int)c;
cout<<i; //output the ascii char

Casting the char as an int causes the integer to take the ascii value of the character.

If you have a pointer/string of chars then simply this will work:

char *c="ABCD";
int i[sizeof(c)],j;
    cout<<i[j]<<" "; //output the ascii chars

I don't see why the above code should work for you.
Leaving you with an array of integers (int i[]) which equal the ascii of the chars in "c".
The last two lines were meant to read

"Leaving you with an array of integers (int i[]) which equal the ascii of the chars in "c".
I don't see why the above code shouldn't work for you. "

As a replacement for atoi, you can use the following function:

DWORD StrToInt(const char *szNum)
   if(szNum == NULL)
      return 0;
   DWORD dwNum = 0;
   BOOL bNegative = FALSE;
   for(int iCnt = 0; szNum[iCnt]; iCnt++)
      if(iCnt == 0 && szNum[0] == '-')
         bNegative = TRUE;
      dwNum *= 10;
      dwNum += szNum[iCnt] - '0';
      dwNum = 0 - dwNum;
   return dwNum;
In case you wish to convert the int into a char then look at the following code.

int i;
char c='A';
char d[3];
i = (int)c;
cout<<"c = "<<c<<endl;
cout<<"i = "<<i<<endl;
cout<<"d = "<<d<<endl;

It converts the original 'A' into an int = 65 (ascii value) then converts this int into a character array = "65".
 Notice how the last array "d" had to be three chars long (char d[3]) to hold the int value.
This is becaus for a char to hold the full ascii set (up to 255) it needs to be 3 chars long for each ascii number.

I hope this has solved your problem.

P.S Sorry for giving you fragments of code. I'm a bit tired and am not thinking straight.

All Courses

From novice to tech pro — start learning today.